RE: [GD-Windows] Stopping a thread cleanly
Brought to you by:
vexxed72
From: Jon W. <hp...@mi...> - 2002-02-17 20:08:53
|
> What are the advantages/disadvantages of exiting a thread based on > WaitForSingleObject with short timeout vs. checking a global variable? Someone should teach me to not mis-read other people's questions. ;-) You have a worker thread, and it's gotten time to shut it down? 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. I think that either suggestion you have (wait on an object with a time-out, versus sleeping) are sub-optimal, because they're polling. Instead, you might want to block (for real) on some primitive until there is work to do, and make sure that primitive gets signalled once work is put in. You may find it easier to use a condition variable for this set-up. Once it's time to exit the thread, you simply set a global (or queue a command, or whatever) and wake the thread up; it'll wake up and notice there's a quit condition (without re-entering the kernel) and go away. 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... Cheers, / h+ |