alternative to pthread_timedjoin_np

c, macos, pthreads

Solution

Producer-consumer queue. Have the threads queue *themselves, and so their results,(if any), to the queue before they exit. Wait on the queue.

No polling, no latency.

With your current design, you would have to join() the returned threads get the valueptr and to ensure that they are destroyed.

Maybe you could sometime move to a real threadpool, where task items are queued to threads that never terminate, (so eliminating thread create/terminate/destroy overhead)?

Problem

I am trying to figure out how to get rid of a reliance on the pthread_timedjoin_np because I am trying to build some code on OSX. Right now I have a Queue of threads that I am popping from, doing that pthread_timedjoin_np and if they dont return, they get pushed back on the queue. The end of the thread_function that is called for each thread does a pthread_exit(0); so that the recieving thread can check for a return value of zero. I thought i might try to use pthread_cond_timedwait() to achieve a similar effect, however I think i am missing a step. I thought I would be able to make worker Thread A signal a condition AND pthread_exit() within a mutex, , and worker Thread B could wake up on the signal, and then pthread_join(). The problem is, Thread B doesn't know which thread threw the conditional signal. Do I need to explicitly pass that as part of the conditonal signal or what? Thanks Derek

Original source

Related problems