如何使用 pthreads 使两个线程相互等待,直到它们各自完成一个循环?
void* th1Fn()
{
while(1)
{
//do something
printf("I'm done");
//signal that i'm done
//wait for thread2 so that I can repeat the cycle
}
}
void* th2Fn()
{
while(1)
{
//do something
printf("I'm done");
//signal that i'm done
//wait for thread1 so that I can repeat the cycle
}
}
最佳答案
您正在寻找pthread_barrier_wait
:http://pubs.opengroup.org/onlinepubs/009695399/functions/pthread_barrier_wait.html
这是此函数的文档示例:http://man7.org/tlpi/code/online/dist/threads/pthread_barrier_demo.c.html
关于c - 同步线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51359095/