Non-blocking pthread_join

后端 未结 8 788
一生所求
一生所求 2020-12-01 08:01

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

相关标签:
8条回答
  • 2020-12-01 08:28

    There is no timed pthread_join, but if you are waiting for other thread blocked on conditions, you can use timed pthread_cond_timed_wait instead of pthread_cond_wait

    0 讨论(0)
  • 2020-12-01 08:32

    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.

    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.

    Start by changing your thinking. It's not a thread that gets stuck, it's what the thread was doing that gets stuck.

    0 讨论(0)
  • 2020-12-01 08:35

    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.

    0 讨论(0)
  • 2020-12-01 08:36

    If you are running your application on Linux, you may be interested to know that:

    int pthread_tryjoin_np(pthread_t thread, void **retval);
    
    int pthread_timedjoin_np(pthread_t thread, void **retval,
                                    const struct timespec *abstime);
    

    Be careful, as the suffix suggests it, "np" means "non-portable". They are not POSIX standard, gnu extensions, useful though.

    link to man page

    0 讨论(0)
  • 2020-12-01 08:45

    I'm not sure what exactly you mean, but I'm assuming that what you really need is a wait and notify mechanism.

    In short, here's how it works: You wait for a condition to satisfy with a timeout. Your wait will be over if:

    • The timeout occurs, or
    • If the condition is satisfied.

    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: POSIX Threads Programming (https://computing.llnl.gov/tutorials/pthreads/).

    I'm also very surprised to see that there's no API for timed join in Pthreads.

    0 讨论(0)
  • 2020-12-01 08:47

    As others have pointed out there is not a non-blocking pthread_join available in the standard pthread libraries.

    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:

    int killed_threads = 0;
    for(i = 0; i < num_threads; i++) {
       int return = pthread_cancel(threads[i]);
       if(return != ESRCH)
          killed_threads++;
    }
    if(killed_threads)
        printf("%d threads did not shutdown properly\n", killed_threads)
    else
        printf("All threads exited successfully");
    

    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).

    That should qualify as a 'simple' workaround.

    0 讨论(0)
提交回复
热议问题