0

Is there any possibility to check if a thread is still running? Using join isn't really what I need, because I want a setup a bit like this:

pthread_t thread_id;
while(true){
    do_stuff();
    if(!thread_id || is_finished(thread_id){
        pthread_create(&thread_id, NULL, do_stuff_with_the_above_stuff, NULL);
    }
    //Else continue to do_stuff() for another round
}
Nazim
  • 406
  • 1
  • 6
  • 20
Potheker
  • 93
  • 8
  • 1
    not really. There is no portable way. The best way is to insure that your thread does not die uncontrollably and always tells you when it exits. An alternative way is to have a heartbeat activity on the thread. There could be some specific way for linux threads or other implementations. – Serge Feb 10 '20 at 19:55
  • Any such mechanism would be *inherently* racy. – EOF Feb 10 '20 at 20:16
  • For glibc, you could use `pthread_tryjoin_np()`. – Ulrich Eckhardt Feb 10 '20 at 20:31
  • Does this answer your question? [How do I determine if a detached pthread is alive?](https://stackoverflow.com/questions/1693180/how-do-i-determine-if-a-detached-pthread-is-alive) – pilcrow Feb 11 '20 at 00:30
  • Perhaps what you're really looking for is a *thread pool*. Even one-thread thread pool would give you the ability to queue tasks for execution by the pool's thread, and a way to determine whether *each task* is complete. Instead of starting a whole new thread for each piece of work, you would queue a task for the thread(s) that already exists. – John Bollinger Feb 11 '20 at 05:42
  • I think you can probably work around it almost all the time, I solved my problem by just using 2 threads and one of them setting a global variable when it finished one round of his work (so when the other one has new data to process) – Potheker Feb 11 '20 at 08:51

0 Answers0