views:

43

answers:

4

I know there is one for multi processes

 waitpid(-1,WNOHANG,NULL)

that is non-blocking function call to check if there is any child process currently working on

But is there any similar lib function to check for multithread?

All i want to do is check if there is any thread currently on, if not reset some global values.

A: 

You could just save the handle of a thread and have a function to check if it is still running. I'm not sure if theres a function but this should work.

Ram Bhat
A: 

pthread_kill(pid, 0) where pid is the thread id that pthread_create has returned can tell you if a thread is still alive. (That is how I understand your question) It returns 0 if the thread is still alive and an error code otherwise.

Jens Gustedt
i think you misunderstood my question. I want to use a function like waitpid(-1,WNOHANG,NULL) for any thread, not specific thread
REALFREE
ah sorry. the page for pthread_join states something as if they where not able to find a compromise about such a function that does a wait for all created threads, and suggest that a programmer should implement this by himself :-( The linux man page to waitpid mentions flags that can be used to handle children created with `clone' so processes and threads. But this is probably not portable to other systems.
Jens Gustedt
A: 

that is non-blocking function call to check if there is any child process currently working on

Wrong. That is a call to check if there is any child process not yet terminated. And it not only checks but also reaps a terminated child, if any. Children might be otherwise in any possible state, like hanging in a deadlock (what on my book is far from being working).

All i want to do is check if there is any thread currently on, if not reset some global values.

Probably you should post here as a question why you want to do it. It sounds that you do something terribly wrong.

If you do not do already pthread_join() for your threads, that means that your threads already do pthread_detach(). If you had no problems adding to your threads pthread_detach() I think there would be no problem to add some extra code to threads to identify that they have (almost) terminated (e.g. sem_post()) so that main() can notice that a thread had terminated (e.g. by calling sem_trylock()).

If portability isn't a requirement, then one can also try query OS number of threads of the process periodically.

Though it is still IMO wrong to have in a program some threads, with undefined life cycle, without any proper sync with main thread.

Dummy00001
A: 

I asked myself something quite similar:

http://stackoverflow.com/questions/3475750/posix-api-call-to-list-all-the-pthreads-running-in-a-process

In your case I would just wrapped up ps -eLF.

msalvadores