<p>I'm coding the shutdown of a multithreaded server.If everything goes as it should all the threads exit by their own, but there's a small chance that a thread gets stuck.In this case it would be convenient to have a non-blocking join so I could do.</p>

<p><strong>Is there a way of doing a non-blocking pthread_join?
Some sort of timed join would be good too.</strong></p>

<p>something like this:</p>

<pre>
foreach thread do
  nb_pthread_join();
    if still running
      pthread_cancel();
</pre>

<p>I can think more cases where a a non-bloking join would be useful.</p>

<p>As it seems there is no such a function so I have already coded a workaround, but it's not as simple as I would like.</p>

[{'answer_id': 73481, 'author': 'Doug T.', 'author_id': 8123, 'author_profile': 'https://Stackoverflow.com/users/8123', 'pm_score': 0, 'selected': False, 'text': '<p>You could push a byte into a pipe opened as non-blocking to signal to the other thread when its done, then use a non-blocking read to check the status of the pipe.</p>\n'}, {'answer_id': 73535, 'author': 'raldi', 'author_id': 7598, 'author_profile': 'https://Stackoverflow.com/users/7598', 'pm_score': 1, 'selected': False, 'text': '<p>The answer really depends on why you want to do this. If you just want to clean up dead threads, for example, it\'s probably easiest just to have a "dead thread cleaner" thread that loops and joins.</p>\n'}, {'answer_id': 73821, 'author': 'Srikanth', 'author_id': 7205, 'author_profile': 'https://Stackoverflow.com/users/7205', 'pm_score': 1, 'selected': False, 'text': '<p>I\'m not sure what exactly you mean, but I\'m assuming that what you really need is a wait and notify mechanism.</p>\n\n<p>In short, here\'s how it works: You wait for a condition to satisfy with a timeout. Your wait will be over if:</p>\n\n<ul>\n<li>The timeout occurs, or</li>\n<li>If the condition is satisfied.</li>\n</ul>\n\n<p>You can have this in a loop and add some more intelligence to your logic. The best resource I\'ve found for this related to Pthreads is this tutorial:\nPOSIX Threads Programming (<a href="https://computing.llnl.gov/tutorials/pthreads/" rel="nofollow noreferrer">https://computing.llnl.gov/tutorials/pthreads/</a>).</p>\n\n<p>I\'m also very surprised to see that there\'s no API for timed join in Pthreads.</p>\n'}, {'answer_id': 81050, 'author': 'shodanex', 'author_id': 11589, 'author_profile': 'https://Stackoverflow.com/users/11589', 'pm_score': 1, 'selected': False, 'text': '<p>There is no timed <code>pthread_join</code>, but if you are waiting for other thread blocked on conditions, you can use timed <code>pthread_cond_timed_wait</code> instead of <code>pthread_cond_wait</code></p>\n'}, {'answer_id': 81261, 'author': 'dmityugov', 'author_id': 3232, 'author_profile': 'https://Stackoverflow.com/users/3232', 'pm_score': 2, 'selected': False, 'text': "<p>If you're developing for QNX, you can use pthread_timedjoin() function.</p>\n\n<p>Otherwise, you can create a separate thread that will perform pthread_join() and alert the parent thread, by signalling a semaphore for example, that the child thread completes. This separate thread can return what is gets from pthread_join() to let the parent thread determine not only when the child completes but also what value it returns.</p>\n"}, {'answer_id': 83788, 'author': 'Frosty', 'author_id': 7476, 'author_profile': 'https://Stackoverflow.com/users/7476', 'pm_score': 2, 'selected': True, 'text': '<p>As others have pointed out there is not a non-blocking pthread_join available in the standard pthread libraries.</p>\n\n<p>However, given your stated problem (trying to guarantee that all of your threads have exited on program shutdown) such a function is not needed.  You can simply do this:</p>\n\n<pre><code>int killed_threads = 0;\nfor(i = 0; i &lt; num_threads; i++) {\n   int return = pthread_cancel(threads[i]);\n   if(return != ESRCH)\n      killed_threads++;\n}\nif(killed_threads)\n    printf("%d threads did not shutdown properly\\n", killed_threads)\nelse\n    printf("All threads exited successfully");\n</code></pre>\n\n<p>There is nothing wrong with calling pthread_cancel on all of your threads (terminated or not) so calling that for all of your threads will not block and will guarantee thread exit (clean or not).</p>\n\n<p>That should qualify as a \'simple\' workaround.</p>\n'}, {'answer_id': 1244687, 'author': 'yves Baumes', 'author_id': 83331, 'author_profile': 'https://Stackoverflow.com/users/83331', 'pm_score': 5, 'selected': False, 'text': '<p>If you are running your application on Linux, you may be interested to know that:</p>\n\n<pre><code>int pthread_tryjoin_np(pthread_t thread, void **retval);\n\nint pthread_timedjoin_np(pthread_t thread, void **retval,\n                                const struct timespec *abstime);\n</code></pre>\n\n<p>Be careful, as the suffix suggests it, "np" means "non-portable". They are not POSIX standard, gnu extensions, useful though.</p>\n\n<p><a href="http://www.kernel.org/doc/man-pages/online/pages/man3/pthread_tryjoin_np.3.html" rel="noreferrer">link to man page</a></p>\n'}, {'answer_id': 7535847, 'author': 'David Schwartz', 'author_id': 721269, 'author_profile': 'https://Stackoverflow.com/users/721269', 'pm_score': 4, 'selected': False, 'text': "<p>The 'pthread_join' mechanism is a convenience to be used if it happens to do exactly what you want. It doesn't do anything you couldn't do yourself, and where it's not exactly what you want, code exactly what you want.</p>\n\n<p>There is no real reason you should actually care whether a thread has terminated or not. What you care about is whether the work the thread was doing is completed. To tell that, have the thread do something to indicate that it is working. How you do that depends on what is ideal for your specific problem, which depends heavily on what the threads are doing.</p>\n\n<p>Start by changing your thinking. It's not a thread that gets stuck, it's what the thread was doing that gets stuck.</p>\n"}]
['https://Stackoverflow.com/questions/73468', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/12661/']