multithreading - pthread_barrier_wait hangs after creation of all threads -


i'm trying write simple program use barrier wait creation of several threads before printing message main.

here's code:

#include <iostream> #include <pthread.h> #include <stdio.h> #include <cstdlib> #include <cstdint>  #define num_threads 8  pthread_barrier_t barrier;  void *threadfun(void *tid) {     intptr_t temp = (intptr_t) tid;     printf("hello thread %d\n", temp); }  int main() {     pthread_t threads[num_threads];     int rc;     pthread_barrier_init(&barrier, null, num_threads);      for(int = 0; < num_threads; ++i) {         rc = pthread_create(&threads[i], null, threadfun, (void *) i);         if(rc)  {             printf("error creating thread %d\n", i);             exit(-1);         }     }      pthread_barrier_wait(&barrier);     printf("hello main!\n");      for(int = 0; < num_threads; ++i) {         pthread_join(threads[i], null);     }     pthread_barrier_destroy(&barrier);      return 0; } 

currently, program prints non-deterministic assortment of "hello thread " statements, , hangs before printing "hello main!"; however, prints 8 thread messages. thus, threads created.

why still hanging?

thanks, erip

the barrier expects waited on num_threads times, 1 thread, main thread, calls pthread_barrier_wait.

if want synchronize main worker threads, you'll need initialize barrier num_worker_threads + 1.


Comments

Popular posts from this blog

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -