Pseudo code:
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 */
}
}
My issue with barriers is how do you deal with threads ending before other threads?
A barrier means every thread has to wait for every other thread.
What techniques are there for ensuring all threads end at the same time?
I tried continueing the loop but ignoring the "thread work", but in this case all 8 threads finish and there's no sensible way to tell the threads "your all done, you can all exit now"
Edit:
Algorithm:
- Run operations on segment of piece of data
- if the thread's segment of piece of data is finished terminate.
- Barrier wait.
- One thread replaces the piece of data with some new data
- repeat 1.
Edit 2:
Is there any elegant way to overwrite the barrier with a barrier that is one size smaller? (without putting a mutex around the barrier)