マルチスレッドをテストするプログラムを作成します。main
関数で aがthread t
作成されます。function D
にある では、thread t
2 つのスレッド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.
この問題を解決してコードを変更するのを手伝ってくれる人はいますか?
これらは私のコードです:
「ダット」
#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
「Dat.cpp」
#include "Dat.h"
Dat::Dat()
{
}
Dat::~Dat()
{
}
void Dat::doOneThing()
{
x = 1;
}
void Dat::doAnotherThing()
{
omutex.lock();
x = 2;
omutex.unlock();
}
「main.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();
}
私の質問のこの文に提案がある場合は、教えてください。修正します。ありがとうございました