创建所有线程后pthread_barrier_wait挂起
pthread_barrier_wait hangs after creation of all threads
我正在尝试编写一个简单的程序来使用屏障等待创建多个线程,然后再从main打印消息。
这是我的代码:
#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 %dn", 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 %dn", 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;
}
目前,我的程序打印一些非确定性的"来自线程的你好"语句,并在打印"Hello from main!但是,它始终打印 8 条线程消息。因此,将创建所有线程。
为什么还挂着?
屏障期望在 NUM_THREADS 次上wait
,但只有一个线程(主线程)实际调用pthread_barrier_wait
。
如果要将 main 与工作线程同步,则需要初始化 NUM_WORKER_THREADS + 1 的屏障。
// 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 %dn", i);
exit(-1);
}
}
printf(" X- - - - - - - - - - - - - - - -Xn");
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- - - - - - - - - - - - - - - -Yn");
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.
相关文章:
- std::condition_variable::wait()如何评估给定的谓词
- std::atomic和std::condition_variable wait,notify_*方法之间的区别
- std::memory_order for std::atomic:<T>:wait
- std::p romise::set_value() 和 std::future::wait() 是否提供内存围栏?
- 对于等待以 std::future wait() 返回的函数的 CPU 使用率或检查标志在循环中休眠一段时间哪个更好?
- std::future::get()或std::future::wait()是std::thread::join()的替
- 在 while 循环中使用 std::condition_variable::wait 是否正确
- future::wait() 是否与 async() 执行线程的完成同步?
- 为什么'wait with predicate'求解条件变量的'lost wakeup'?
- 线程锁定互斥锁的速度比 std::conditional_variable::wait() 快
- 我们在哪里可以使用std::barrier over std::latch
- deadline_timer::wait 是否让位于其他任务
- std::future::wait 是内存障碍吗?(我无法解释这种数据竞赛)
- 如何退出 QThread::wait()
- 当Qtest :: Qwait(..)成功时,Qsignalspy :: Wait(..)失败
- 如何在Wait()C++期间同时调用另一个函数
- 条件变量的"wait"函数在提供谓词时导致意外行为
- 在 UWP 应用程序中,future.wait() 在尝试同步来自异步方法的响应时继续等待
- 条件变量wait引发异常
- 为什么 future::wait() 块