title: Semaphores as a diamond
body: <p>My homework was to perform waits and signals on an array of semaphores in a diamond pattern.</p>
  
  <p>Here is the pattern:</p>
  
  <pre><code>    08
    06  07  
  03  04  05
    01  02
      00
  
  Thread #08 cannot join the diamond until threads #06 and #07 are both in position.
  Thread #07 cannot join until threads #04 and #05 are both in position.
  Thread #06 cannot join until threads #03 and #04 are both in position. and so on...
  </code></pre>
  
  <p>So far my thinking has resulted in the following ugly algorithm:</p>
  
  <pre><code>   if (me==0) {
         wait_sem (tCount[1]) ;
         wait_sem (tCount[2]) ;
         signal_sem (tCount[0]) ;
     }
     if (me==1) {
         wait_sem (tCount[3]) ;
         wait_sem (tCount[4]) ;
     }
     if (me==2) {
         wait_sem (tCount[4]) ;
         wait_sem (tCount[5]) ;
     }
     if (me==3) {
         wait_sem (tCount[6]) ;
     }
     if (me==4) {
         wait_sem (tCount[6]) ;
         wait_sem (tCount[7]) ;
     }
     if (me==5) {
         wait_sem (tCount[7]) ;
     }
     if (me==6) {
         wait_sem (tCount[8]) ;
     }
     if (me==7) {
         wait_sem (tCount[8]) ;
     }
  </code></pre>
  
  <p>Is there a cleaner method of doing this? I heard <code>switch</code> but I have never used it before so if anyone suggests it please provide me with an explanation and or example. Thanks a lot for all input.</p>