因此,我知道您可以在C中创建障碍来控制线程程序的流。您可以初始化屏障,让线程使用它,然后销毁它。但是,我不确定是否可以重复使用相同的屏障(例如,如果是循环的话)。或者你必须使用一个新的屏障作为第二个等待点?例如,下面的代码是否正确(重用相同的屏障)?
#include <pthread.h>
pthread_barrier_t barrier;
void* thread_func (void *not_used) {
//some code
pthread_barrier_wait(&barrier);
//some more code
pthread_barrier_wait(&barrier);
//even more code
}
int main() {
pthread_barrier_init (&barrier, NULL, 2);
pthread_t tid[2];
pthread_create (&tid[0], NULL, thread_func, NULL);
pthread_create (&tid[1], NULL, thread_func, NULL);
pthread_join(tid[0], NULL);
pthread_join(tid[1], NULL);
pthread_barrier_destroy(&barrier);
}发布于 2016-03-30 20:06:26
是的,它们是可重复使用的。The 手册页说
当所需的线程数已经调用pthread_barrier_wait()时,...the屏障将被重置为引用它的最近的pthread_barrier_init()函数所具有的状态。
https://stackoverflow.com/questions/36318516
复制相似问题