pthread_barrier_wait hangs after creation of all threads

1.9k views Asked by At

I'm trying to write a simple program to use a barrier to wait for the creation of several threads before printing a message from the main.

Here's my 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 from thread %d\n", temp);
}

int main()
{
    pthread_t threads[NUM_THREADS];
    int rc;
    pthread_barrier_init(&barrier, NULL, NUM_THREADS);

    for(int i = 0; i < 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 from main!\n");

    for(int i = 0; i < NUM_THREADS; ++i) {
        pthread_join(threads[i], NULL);
    }
    pthread_barrier_destroy(&barrier);

    return 0;
}

Currently, my program prints some non-deterministic assortment of "Hello from thread " statements, and hangs before printing "Hello from main!"; however, it always prints 8 thread messages. Thus, all the threads are created.

Why is it still hanging?

2

There are 2 answers

0
pilcrow On BEST ANSWER

The barrier expects to be waited on NUM_THREADS times, but only one thread, the main thread, actually calls pthread_barrier_wait.

If you want to synchronize main with your worker threads, you'll need to initialize the barrier for NUM_WORKER_THREADS + 1.

0
Henke - Нава́льный П с м On
//  q30941549.cpp
// https://stackoverflow.com/questions/30941549
#include <iostream>
#include <pthread.h>
#define NUM_THREADS 8
pthread_barrier_t barrier;

void *threadFun(void *tid) {
  printf("Work done by thread %ld BEFORE the barrier.\n", (long)tid);
  pthread_barrier_wait(&barrier);
  printf("Hello from thread %ld AFTER the barrier.\n", (long)tid);
  pthread_exit(NULL);
}

int main() {
  pthread_t threads[NUM_THREADS];
  int rc;
  pthread_barrier_init(&barrier, NULL, NUM_THREADS+1);
  for(int i = 0; i < NUM_THREADS; ++i) {
    rc = pthread_create(&threads[i], NULL, threadFun, (void *)(long)i);
    if(rc)  {
      printf("Error creating thread %d\n", i);
      exit(-1);
    }
  }
  printf(" X- - - - - - - - - - - - - - - -X\n");
  printf("main thread BEFORE the barrier.\n");
  printf(" = = BEFORE barrier = BEFORE barrier = =\n");
  pthread_barrier_wait(&barrier);
  printf(" = = AFTER barrier = = AFTER barrier = =\n");
  printf("Hello from main! AFTER the barrier.\n");
  printf(" Y- - - - - - - - - - - - - - - -Y\n");
  for(int i = 0; i < NUM_THREADS; ++i) {
    pthread_join(threads[i], NULL);
  }
  printf("* - * - * - * - * - * - * - * - * - * - * - * - * - *\n");
  printf("This line is ALWAYS last. Only main lives after join.\n");
  return EXIT_SUCCESS;
}

To compile and run:

g++ -pthread q30941549.cpp && ./a.out # Linux
g++ -pthread q30941549.cpp && ./a.exe # MSYS2, Windows

If you try running it a few times, you might see something like:

Work done by thread 0 BEFORE the barrier.
Work done by thread 3 BEFORE the barrier.
Work done by thread 6 BEFORE the barrier.
Work done by thread 4 BEFORE the barrier.
Work done by thread 1 BEFORE the barrier.
Work done by thread 5 BEFORE the barrier.
Work done by thread 7 BEFORE the barrier.
Work done by thread 2 BEFORE the barrier.
 X- - - - - - - - - - - - - - - -X
main thread BEFORE the barrier.
 = = BEFORE barrier = BEFORE barrier = =
 = = AFTER barrier = = AFTER barrier = =
Hello from main! AFTER the barrier.
 Y- - - - - - - - - - - - - - - -Y
Hello from thread 6 AFTER the barrier.
Hello from thread 4 AFTER the barrier.
Hello from thread 1 AFTER the barrier.
Hello from thread 5 AFTER the barrier.
Hello from thread 7 AFTER the barrier.
Hello from thread 2 AFTER the barrier.
Hello from thread 0 AFTER the barrier.
Hello from thread 3 AFTER the barrier.
* - * - * - * - * - * - * - * - * - * - * - * - * - *
This line is ALWAYS last. Only main lives after join.