C++ this as thread parameter, variables unavailable
- by brecht
I have three classes:
class Rtss_Generator {
int mv_transfersize;
}
class Rtss_GenSine : Rtss_Generator
class Rtss_GenSineRpm : Rtss_GenSine
Rtss_GenSine creates a thread in his constructer, it is started immediatly and the threadfunction is off-course declared static, waiting for an event to start calculating.
the problem: all the variables that are accessed through the gen-pointer are 0, but it does not fail. Also, this-address in the constructer and the gen-pointer-address in the thread are the same, so the pointer is ok.
this code is created and compile in visual studio 6.0 service pack 2003
ORIGINAL CODE
no thread
Rtss_GenSine::getNextData() {
//[CALCULATION]
//mv_transferSize is accessible and has ALWAYS value 1600 which is ok
}
NEW CODE
Rtss_GenSine::Rtss_GenSine() {
createThread(NULL, threadFunction, (LPVOID) this,0,0);
}
Rtss_GenSine::getNextData() {
SetEvent(startCalculating);
WaitForSingleObject(stoppedCalculaing, INFINITE);
}
DWORD Rtss_GenSine::threadFunction(LPVOID pParam) {
Rtss_GenSine* gen = (Rtss_GenSine*) pParam;
while(runThread) {
WaitForSingleObject(startCalculating, INFINITE);
ResetEvent(startCalculating)
//[CALCULATION]
//gen->mv_transferSize ---> it does not fail, but is always zero
//all variables accessed via the gen-pointer are 0
setEvent(stoppedCalculaing)
}
}