November 9th, 2010, 08:59 AM
[RESOLVED] Start thread within a object, and have acces to it aswell
I found a similar thread that talked more or less about the same issue but it was 6 years old so I decided to start this thread.
Before we start I have read the: http://www.codeguru.com/forum/showthread.php?t=312452 FAQ but that didn't help much.
I have the following:
A .dll made in vc++ 2010. This library contains many classes of which one needs to create an actual thread to work called CSoftPLC (guess what it does :P). This method has the function "StartPLC()" which creates the thread, and "StopPLC()"... Well you get the picture. The library compiles perfectly, not even warnings emerge and the thread also starts fine. The only thing that doesn't work is that the thread cannot acces the class that created it eventhough i pass it as a pointer as the lpArg parameter.
The class SoftPLC has a bunch of flags indicating if the thread encountered an error , some functions that it needs to execute etc and is therefor quite large. Here is a snippet that is concerned:
I see the problem of the thread not be´ng allowed acces to the private members of the object creating it (and passing itself as a parameter) as the ThreadStart requires a "static" type function. Is there a way that the thread can access these members whithout making them public? Those members need to be and stay protected if possible and are only accessable with functions.
.h File: (propperly exported etc)
class DECLDIR CSoftPLC
int _nErrNo ;
bool _bRunThread; <-- if this goes low the thread should stop(normally)
static DWORD _ThreadProc(LPVOID lpArg);
#pragma region Public Routines
this->_bRunThread = true;
this->_hProcThread = CreateThread(NULL, 0x100,
if( this->_hProcThread = NULL)
this->_bRunThread = false;
#pragma region PLC Proces
DWORD CSoftPLC::_ThreadProc(LPVOID lpArg)
CSoftPLC* myPLC = (CSoftPLC*)lpArg;
myPLC = (CSoftPLC *)lpArg;
unsigned int i = 1;
while( (myPLC->_bRunThread) && (i > 0))
myPLC->_nState |= RUNNING; //Set flag running
cout << "blabla:" << i;
myPLC->_nState &= ~RUNNING; //Reset flag running
Tags for this Thread
Click Here to Expand Forum to Full Width