pthread_barrier_wait在创建所有线程后挂起

时间:2015-06-19 15:09:41

标签: multithreading pthreads barrier

我尝试编写一个简单的程序,在从主程序打印消息之前使用屏障等待创建多个线程。

这是我的代码:

#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;
}

目前,我的程序打印了一些非确定性的分类&#34; Hello来自线程&#34;声明,并在打印前挂起&#34; Hello来自main!&#34 ;;但是,它总是打印8个线程消息。因此,创建了所有线程。

为什么还挂着?

2 个答案:

答案 0 :(得分:3)

屏障期望在NUM_THREADS次wait次,但主线程只有一个线程实际调用pthread_barrier_wait

如果要将main与工作线程同步,则需要初始化NUM_WORKER_THREADS + 1的屏障。

答案 1 :(得分:0)

//  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;
}

要编译并运行:

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

如果尝试运行几次,可能会看到类似以下内容的

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.