title: how to handle barriers with threads quitting
body: <p><strong>Pseudo code:</strong></p>
  
  <pre><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 &lt; 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 */
      }
  }
  </code></pre>
  
  <p>My issue with barriers is how do you deal with threads ending before other threads?</p>
  
  <p>A barrier means every thread has to wait for every other thread.</p>
  
  <p>What techniques are there for ensuring all threads end at the same time?</p>
  
  <p>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"</p>
  
  <p><strong>Edit:</strong></p>
  
  <p>Algorithm:</p>
  
  <ol>
  <li>Run operations on segment of piece of data</li>
  <li>if the thread's segment of piece of data is finished terminate.</li>
  <li>Barrier wait.</li>
  <li>One thread replaces the piece of data with some new data</li>
  <li>repeat 1.</li>
  </ol>
  
  <p><strong>Edit 2:</strong></p>
  
  <p>Is there any elegant way to overwrite the barrier with a barrier that is one size smaller? (without putting a mutex around the barrier)</p>