僞代碼障礙:如何處理與線程退出
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個線程完成,有沒有合理的方式來告訴線程「你都做,你現在可以全部退出」
編輯:
算法:
- 上的數據塊
- 段運行操作,如果數據塊的線程的段完成termina TE。
- 障礙等待。
- 一個線程替換一塊數據有一些新的數據
- 重複1
編輯2:
有覆蓋一個屏障,一個阻隔任何優雅的方式尺寸較小? (無需在屏障上放置互斥鎖)
甚至沒有不雅的方式:),你不能複製另一個障礙或在相同的障礙上多次調用'pthread_barrier_init'。 – chill