How can I wait for any/all pthreads to complete?

后端 未结 5 1441
深忆病人
深忆病人 2020-12-08 07:15

I just want my main thread to wait for any and all my (p)threads to complete before exiting.

The threads come and go a lot for different reasons, and I really don\'

5条回答
  •  广开言路
    2020-12-08 07:44

    Thanks all for the great answers! There has been a lot of talk about using memory barriers etc - so I figured I'd post an answer that properly showed them used for this.

    #define NUM_THREADS 5
    
    unsigned int thread_count;
    void *threadfunc(void *arg) {
      printf("Thread %p running\n",arg);
      sleep(3);
      printf("Thread %p exiting\n",arg);
      __sync_fetch_and_sub(&thread_count,1);
      return 0L;
    }
    
    int main() {
      int i;
      pthread_t thread[NUM_THREADS];
    
      thread_count=NUM_THREADS;
      for (i=0;i

    Note that the __sync macros are "non-standard" GCC internal macros. LLVM supports these too - but if your using another compiler, you may have to do something different.

    Another big thing to note is: Why would you burn an entire core, or waste "half" of a CPU spinning in a tight poll-loop just waiting for others to finish - when you could easily put it to work? The following mod uses the initial thread to run one of the workers, then wait for the others to complete:

      thread_count=NUM_THREADS;
      for (i=1;i

    Note that we start creating the threads starting at "1" instead of "0", then directly run "thread 0" inline, waiting for all threads to complete after it's done. We pass &thread[0] to it for consistency (even though it's meaningless here), though in reality you'd probably pass your own variables/context.

提交回复
热议问题