伪代码:
void * thread_start(void *arg) {
while (1) {
/* for each column. Only run columns the thread_num
is assigned to */
column_count = thread_num;
for (; column_count < dim - 1; column_count+=threads) {
/* do thread work for data chunk */
}
/* barrier */
barrier_result = pthread_barrier_wait(barrier);
if (not_finished == 0) {
/* finished */
break;
/* break while loop, end thread.
The barrier is now broken because it is no longer
getting called by this thread */
}
/* we are not finished, loop around and
do thread work on next data chunk */
}
}
我对障碍的问题是如何处理在其他线程之前结束的线程?
屏障意味着每个线程都必须等待所有其他线程。
有哪些技术可以确保所有线程同时结束?
我尝试继续循环但忽略“线程工作”,但在这种情况下,所有 8 个线程都完成了,并且没有明智的方法告诉线程“你都完成了,你现在都可以退出”
编辑:
算法:
- 对一段数据运行操作
- 如果线程的数据段完成终止。
- 障碍等待。
- 一个线程用一些新数据替换这条数据
- 重复 1。
编辑2:
有没有什么优雅的方法可以用小一号的屏障覆盖屏障?(没有在屏障周围放置互斥锁)