Calling a non-static class function on another thread
I rewriting some code that i written a long time ago.
The code is a class that start another worker thread with AfxBeginThread
. When the thread ends, it needs to return it work to the calling class开发者_StackOverflow社区.
Actually when the thread ends it send a message by PostMessage
with its results to the called class.
But this way is really dependent of MFC, and to do this my class have to implement all the MFC stuffs.
May be correct if instead of send a message it directly call a non-static method of this class ?
Rather than trying to call a method directly (which will introduce a whole new set of threading problems of its own), try using the native Win32 ::PostMessage()
instead of the MFC implementation of the same function. Any thread can call ::PostMessage()
to deliver a message to another thread safely.
It sounds as though you want to use regular threading primitives, not window messaging primitives.
Which version of AfxBeginThread
are you using? If you pass it a class instance, you should be able to access the members of that class directly once you know its finished running. If you passed it a function pointer, you can pass any class pointer in with the lParam parameter, then use that as a communication context.
You just want to make sure that when you access the class you do it in a thread safe manner. If you wait till the thread has ended you should be fine. Otherwise you could use Critical Sections or Mutexes. See the MSDN article on thread synchronization primitives for more info.
精彩评论