Click here to Skip to main content
15,907,687 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Ok, I have googled this and everything which made me do this the way I try in the first place. The actual thread is defined as friend because AfxBeginThread only takes static functions, and a pointer to the class instance is passed as argument.
But I just can't see why this is wrong.
Really glad for an explanation / correct way to do it.

Now, I wrote this as a maximally stripped down version and like this it obviously doesn't make a lot of sense.
But what I really want to do is have a thread which, as argument, receives a pointer to a class instance, and can thus manipulate class variables. Sounds easy, no?

Well, the way I tried to do it, it compiles but doesn't run, following error in the line printed in bold
Unhandled exception at 0x0041186d in simpleThread.exe: 0xC0000005: Access violation reading location 0xccccccd0.


class testClass{
public:
	
	testClass();

	CEvent* pEvent;
	CWinThread* pThread;
	friend UINT ThreadProc(LPVOID lpParameter);

	void startThread();
        void sendEvent();
};


int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{

   testClass myInstance;
   myInstance.startThread();
   Sleep(2000);
   myInstance.sendEvent();
   return 0;
}

UINT ThreadProc( LPVOID lpParameter )
{
	testClass* instance = (testClass*)(lpParameter);
	WaitForSingleObject( instance->pEvent->m_hObject, INFINITE );
	return 0L;
}

testClass::testClass(){
	CEvent* pEvent = new CEvent( FALSE, FALSE );
}

void testClass::startThread()
{
	pThread = ::AfxBeginThread( ThreadProc, this, 0, 0, 
                                    CREATE_SUSPENDED, NULL);
	pThread->m_bAutoDelete = FALSE; 
	pThread->ResumeThread();
}
void testClass::sendEvent()
{
    pEvent->SetEvent();
}


P.S.: My code is based on http://msdn.microsoft.com/en-us/library/efk30beh%28v=vs.80%29.aspx[^] which is very similar, except they don't try to wrap it all up in a class.

Many thanks for solutions / suggestions.
Posted
Updated 25-May-11 16:51pm
v2

1 solution

I don't think you should approach your problem in this manner... Why would you ever want an independent thread to directly manipulate your data members of a different class? You need to learn to use thread safe ways of doing this.

One more thing...
NEVER use WaitForSingleObject() with an INFINITE timeout!
 
Share this answer
 
v2
Comments
tschoni 26-May-11 0:07am    
Of course the INFINITE timeout is just here because this is meant as an example.

About the better ways to solve this:
The idea is to write a class which handles reading GPS data from a serial bus.
The whole idea behind encapsulating this in a class was thread safety to begin with.
One thread as part of the class should be reading data.
The data should only be accessible through a class member function, which implicitly ensures thread safety without the caller having to worry about it.

I see that the "friend" solution is kind of drifting apart from this.

Maybe I should use a static member function, that receives a "this" pointer as argument, instead of a friend function.

Maybe you suggest something else?
Albert Holguin 26-May-11 10:04am    
this sounds like you need a persistent thread instead of a worker thread... in MFC, a persistent thread is referred to as a UI thread (bad name, I know), you can derive a thread from CWinThread and message any data you need over to it, when its done processing, it can message back results, if there's shared memory required, it should be allocated on the heap if practical (and deallocated after processing) or some sort of shared memory space should be set up and controlled with a mutex.
tschoni 31-May-11 1:42am    
thanks for the suggestions
however, i feel the "easy" way to do is is still using a working thread, I dont see why a UI thread should be used, can you maybe explain?
because the "main" purpose is not really sending messages back and forth but just "publishing" received data in a fixed time interval.
the only message it has to somehow receive is when to terminate (which will be only when starting & shutting down pc)
Albert Holguin 2-Jun-11 20:09pm    
wait... why are you threading that then? ...if all you need is to publish at a fixed interval you need a timer not a thread...
tschoni 2-Jun-11 20:17pm    
i think i do, no?
other stuff is happening next do this.
and every so-and-so many seconds i update the gps signal that is used by other parts of the software

oh, and the signal frequency is basically given by the gps
maybe that's why threading makes sense. thread just sits and waits until new signal arrives, interprets it and saves it to a variable accessible by other parts of the software

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900