thread invoke Synch marshalling

P

Paul

Hi all,



I'm trying to trigger an event from threadB which will be processed by
threadA. It sounds simple, but I cannot seem to find any documentation about
this anywhere.



This is very much like what the windows controls have with InvokeRequired
and then invoke from the current thread. Although I can see that it's not
running on threadA by comparing threadIDs I can't find how to invoke the
method from threadA instead of threadB. I want to be able to do this without
using wait methods to wait for events, instead just pushing the event onto
that threads stack.



Any Ideas?
 
B

Ben Voigt

Paul said:
Hi all,



I'm trying to trigger an event from threadB which will be processed by
threadA. It sounds simple, but I cannot seem to find any documentation
about this anywhere.



This is very much like what the windows controls have with InvokeRequired
and then invoke from the current thread. Although I can see that it's not
running on threadA by comparing threadIDs I can't find how to invoke the
method from threadA instead of threadB. I want to be able to do this
without using wait methods to wait for events, instead just pushing the
event onto that threads stack.

The canonical way of doing this is by posting to the thread's message queue.
While you could theoretically suspend the thread, update its context, etc,
etc, that is 99.999% certain to create bugs. What if that thread holds a
lock on a shared resource? There are no synchronization primitives provided
to help you with this. Your event handlers would have to be written like
interrupt service routines, and even so, most API calls would not be
available to you since they modify state that would trash whatever work the
thread was doing. Even then, the other thread is not required to run
instantly, but could be scheduled later. If you really need synchronous
events, make the call from your own thread and use proper synchronization.

What I think you'd ultimately find is that there are certain places where
the thread cannot afford to be interrupted. Win32 API already provides for
context-switching and provides critical sections, mutexes, semaphores,
events to help you manage shared state. Don't try to re-implement all that
yourself.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top