Templates, Tools and Utilities for Clarion Developers
|
|
|
|
Icetips Article
Back to article list
Search Articles
Add Comment
Printer friendly
Direct link
Windows API: About thread management
2006-06-16 -- Jim Kane
> I'm a bit embarassed to ask as I know this has been asked about a
> million times before but I've never had the need, never anticipated
> I'd need it, but... I do need it What I need to do is close all
> active threads in an app. I need this to reset a global filtering
> variable and I'd rather close all threads than change the variable and
> post notifications or events to all open threads. I'd rather not
> spend time on writing this myself so my question is: Is there
> something (must have source) that can do this for me available free or
> commercial? I don't want to just post x number of Event:CloseWindow
> to an unknown number of threads, I want to be able to close exactly
> all of the running threads, not threads that might or could be running
> when the moon is full or something;)
There are two general methods: global variables (Q or otherwise) + critical
sections vs api method using anycombo you want of hthreads and event objects
(hEvent) - no critical sections required. There isn't a whole bunch of
advantages of one over the other. IMO the api method is more foolproof/reliable.
You already know the global variable drill - it relys on you setting and
later clearing something global after getting thru a critical section.
For the api method, instead of calling start() call the createThread() api +
attachthreadtoclarion() (or use start and after the start() call
getcurrentthread()). Either way you end up with a hthread.
if you have an hthread you can call WaitForSingleObject() at any time to
determine if the thread is alive or not. The nice thing about this method
is it's fool proof and doesnt rely on the thread clearing a global and no
need for critical sections.
If you need to tell multiple threads to close down there are again several
approaches
1. each thread checks a global variable ( with critical section ) and if the
global variable says it's time to close down it closes down. To tell the
caller the thread has ended, it must clear a global variable with critical
section or the caller has to have an hthread for the thread to test.
WaitformultipleObjects can be a very convenient way to wait for multiple
threads to all close. this method requires the thread to check the global
for a signal to close frequently.
2. Instead of using a global variable to signal to the threads use clarion
post() - this requires the thread to have a window and be cycling the accept
loop so the event gets processed.
3. Instead of using a global variable to singal to the threads use an event
object - each thread checks the event object and closes when the event
object signals - no critial sections. the threads have to check the hevent
to know it's time to close.
Again there isnt a clear advantage to any of these other than if the thread
doesnt have a window or doesnt cycle the accept loop frequently (often
modal) then it's better to have the thread check a global variable or event
object. When I write a service I virtually always use this event object
method so the thread that receives notifications from windows to close down
can signal an event object and indirectly cause all the services threads to
close whether or not the threads have windows open or not.
So no matter what you choose it can work but it's a little work. Asside
from special circumstances like no windows or non-modal behavior for a
thread it probably doesnt matter a whole bunch which you pick.
Jim Kane
Today is December 4, 2024, 2:34 am This article has been viewed 35272 times.
Google search
has resulted in 110 hits on this article since January 25, 2004.
Back to article list
Search Articles
Add Comment
Printer friendly
|
|
|