-
November 9th, 2010, 08:59 AM
#1
[RESOLVED] Start thread within a object, and have acces to it aswell
Hello everyone,
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:
Code:
.h File: (propperly exported etc)
class DECLDIR CSoftPLC
{
protected:
int _nErrNo ;
int _nState;
int _nWDTtime;
int _nCycleTime;
int _nActualCycleTime;
//Threading items
bool _bRunThread; <-- if this goes low the thread should stop(normally)
DWORD ThreadID;
HANDLE _hProcThread;
static DWORD _ThreadProc(LPVOID lpArg);
public:
CSoftPLC(void);
int StartPLC(void);
int StopPLC(void);
void Dispose(void);
virtual ~CSoftPLC(void);
};
.cpp file:
#pragma region Public Routines
int CSoftPLC::StartPLC(void)
{
this->_bRunThread = true;
this->_hProcThread = CreateThread(NULL, 0x100,
(LPTHREAD_START_ROUTINE)CSoftPLC::_ThreadProc,
(LPVOID)this,
0,
&ThreadID);
if( this->_hProcThread = NULL)
{
return 1;
}
return 0;
}
int CSoftPLC::StopPLC(void)
{
if(this->_hProcThread)
{
this->_bRunThread = false;
return 0;
}
return 1;
}
#pragma endregion
#pragma region PLC Proces
DWORD CSoftPLC::_ThreadProc(LPVOID lpArg)
{
//Variables
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;
i++;
}
myPLC->_nState &= ~RUNNING; //Reset flag running
return NULL;
}
#pragma endregion
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.
T.Y.I.A.
Crash.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|