我写了一个程序来测试多线程。在main
函数thread t
中创建了 a。in function D
,即 in ,将创建thread t
两个线程。该函数在. 在一个成员函数中会被调用。在一个成员函数中会被调用。tt
ttt
Process
thread ttt
Process
doAnotherThing
class Dat
thread tt
doOneThing
当我调试这个程序时,出现了一个错误:An exception (first chance) at 0x76f6f9d2 in Boost_Mutex.exe: 0xC0000008: An invalid handle was specified.
有时会发生此错误而不是上面的错误:
Run-Time Check Failure #2 - Stack around the variable 'oDat' was corrupted.
谁能帮我解决这个问题并修改代码?
这些是我的代码:
“达.h”
#pragma once
#ifndef DAT_H
#define DAT_H
#include <boost\thread\thread.hpp>
using namespace std;
class Dat
{
public:
Dat();
~Dat();
void doOneThing();
void doAnotherThing ();
private:
boost::mutex omutex;
int x;
};
#endif // DAT_H
“数据.cpp”
#include "Dat.h"
Dat::Dat()
{
}
Dat::~Dat()
{
}
void Dat::doOneThing()
{
x = 1;
}
void Dat::doAnotherThing()
{
omutex.lock();
x = 2;
omutex.unlock();
}
“主.cpp”
#include "Dat.h"
#include <boost\function.hpp>
struct Parameter // the Parameters of function Process and D
{
Dat* pDat;
};
void Process(void*pParam)
{
// init the parameter
parameter* pUserParams = (parameter*)pParam;
pUserParams->pDat->doAnotherThing();
}
void D(void* pParam)
{
// init the parameter
parameter* pUserParams = (parameter*)pParam;
boost::function<void()> f;
boost::thread ttt(Process, (void*)&pParam);
f = boost::bind(&Dat::doOneThing, pUserParams->pDat);
// the capture thread will be started
boost::thread tt(f);
ttt.join();
tt.join();
}
void main()
{
Dat oDat;
parameter pPara ;
pPara.pDat = &oDat;
boost::thread t(D,(void*)&pPara);
t.join();
}
如果您对我的问题陈述有任何建议,请告诉我,我会修改它。谢谢