RE: [GD-Windows] Stopping a thread cleanly
Brought to you by:
vexxed72
From: Brian H. <bri...@py...> - 2002-02-17 20:12:43
|
> Somehow, you're giving this worker thread instructions, so I'd > suggest sending a shutdown message the same way you send other > commands to the thread. Actually, I'm not giving it instructions -- it's completely autonomous based on the parameter passed to it which is a pointer to the shared data it needs to operate on. It doesn't have any communication mechanism with the main thread except for the synchronization objects. > If the worker thread is supposed to asynchronously "pick up" work > every so often (sometimes necessary) then you should use the > time- out on the wait operation as your throttle for when to look for > more work. That way, you can signal this event (or whatever) when > it's time to exit, and the thread will immediately wake up and > exit, instead of you having to wait for however long the sleep > operation would take. You can even use the condition of "waiting > for this object did NOT return time-out" as your exit condition, > for a race condition free way of doing it :-) > Finally, the most dangerous thing with exiting threads is having > other threads wait for those threads; unless you design carefully, > it's very easy to get into deadlock that way... Yeah, that can be nasty, but thankfully it's only a single thread and only one place kills it, basically via: SetEvent( event ); while ( !gThreadDead ) ; //pray that gThreadDead actually gets set DeleteSharedData(); Brian |