Results 1 to 4 of 4
Hi,
In one of my applications, I am using threads. Threads are created
using the pthread_create function call. I want to monitor the health status of all the threads.
What ...
- 07-23-2008 #1Just Joined!
- Join Date
- Jul 2008
- Posts
- 2
Threads
Hi,
In one of my applications, I am using threads. Threads are created
using the pthread_create function call. I want to monitor the health status of all the threads.
What is the mechanism in Linux to wait for multiple threads Simultaneously
?
If you have any idea please reply. Thanks in advance.
Regards
Srinivas
- 07-23-2008 #2
c threads for one million alex:
> whatis pthread_join
pthread_join (3p) - wait for thread termination
- 07-24-2008 #3Just Joined!
- Join Date
- Jul 2008
- Posts
- 2
- 07-24-2008 #4
It all depends on what you mean by checking the "health" of a thread.
There is no reliable way to check for a thread which has blown up with, for example, a segment violation. All you can do is debug your code so there are no such fatal errors.
Assuming, then, that there are no relevant bugs in your code, what you can do is let the primary thread create some sort of control block for each of the other threads. Let's say that one of the fields in the control block is an int called still_running. Set that field nonzero before creating the thread. Then pass the address of the thread's control block as the final parameter of the pthread_create() call for that thread.
The thread, just before exiting, should set that field to zero. No other thread should touch that field, so there are no synchronization issues involved.
If you wish to wait for any or all of your threads to exit, create an inner loop which cycles through all the thread control blocks you've created, looking for one whose related still_running has been set to zero. If you've found one, exit the loop, or exit the loop if none has been found. If one has been found, use pthread_join(). (You can skip the pthread_join() step if you detach each thread.)
Then, if you found a completed thread, do whatever processing you need to do for that thread.
If no thread has been found to be completed, wait a second (or a smaller period of time if you wish).
Wrap the inner loop described above, the finished-thread processing, and the wait in an outer loop. Exit the outer loop when no threads remain.
Embroider this approach as necessary.--
Bill
Old age and treachery will overcome youth and skill.


Reply With Quote
