title: Pthreads, trying to use semaphores to print out sections of numbers from multiple threads
body: <p>The problem I'm having is this:  I have my "parent" going through a for loop, from 0 to 4.  On each iteration, I have 3 threads that I want to print out their "segment" of 12 numbers (0 to 11).</p>
  
  <p>For example, when the parent loop is at 0,  thread 0 will print out 0, thread 1 will print out 4, thread 2 will print out 8.</p>
  
  <p>When the parent loop is at 1, thread 0 will print out 1, thread 1 will print out 5, thread 2 will print out 9.  </p>
  
  <p>So ideally the output would be 0 1 2 3 4 5 6 7 8 9 10 11 .  I know that you can't determine when threads run, so it won't be exactly in that order, but I at least want to get 0 1 and 2 printed before the threads print 3 4 or 5.  </p>
  
  <p>That's what I'm having problems with.  It seems like one thread or another gets left in the dust and doesn't print out its segment, or doesn't fully print out its segment along with the other threads.  This is a problem I'm doing to try and fully understand semaphores.</p>
  
  <p>I have two semaphores, one that blocks the parent (Producer?) when the threads are working, and one that blocks each thread until the producer increments to the next index.  I thought that in this way I could force them to wait for each other to get finished before continuing, but for some reason I'm having issues.  </p>
  
  <p>Here's my code: </p>
  
  <pre><code>#include &lt;semaphore.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;pthread.h&gt;
  
  #define maxNum 12
  
  sem_t parentSem;
  sem_t threadSem;
  int finishedThreads;
  int done = 0;
  int stuff[12];
  int i;
  
  void* threadFunction(int m){
      int printNum;
      int baseNum;
      //Determine the value the thread should start at
      baseNum = (double)(maxNum/3) * m;
      while(!done){ //ensure thread doesn't exit before parent is done with whole loop
          //wait for parent to increment
          sem_wait(&amp;threadSem);
          printNum = baseNum + i;
  
              //keep track of how many threads are finished to let parent continue
          finishedThreads++;
          if(finishedThreads == 3){
                      //let parent continue if all threads are finished
              sem_post(&amp;parentSem);
          }
      }
  
  }
  
  int main(int argc, char** argv[]){
  
      sem_init(&amp;parentSem, 0, 1);
      sem_init(&amp;threadSem, 0, 0);
      int rc;
      pthread_t threads[3];
      int l; 
      for(l = 0; l &lt; 12; l++){
          stuff[l] = l;
      }
      int j;
      for(j = 0; j &lt; 3; j++){
          rc = pthread_create(&amp;threads[i], NULL, threadFunction, (void*) j);
  
      }
      int k;
      for(i = 0; i &lt; 4; i++){
          sem_wait(&amp;parentSem); //wait for children here (initially sem set to 1)
          finishedThreads = 0; //set finished thread counter to 0
          for(k = 0; k &lt; 3; k++){
              //increment thread semaphore to 3 so each thread can run
              sem_post(&amp;threadSem);
          }
      }
      for(i = 1; i &lt; 3; i++){
          pthread_join(threads[i], NULL);
      }
  
  
  
  }
  </code></pre>
  
  <p>How can I ensure that all threads run prior to the parent incrementing?  How can I make sure that all threads run per "round" without any being stuck behind? Sometimes the same thread runs twice instead of each thread running once..... help?</p>
  
  <p>Thank you all very much for your help.</p>
  
  <p>EDIT: New code state: (Thread function)</p>
  
  <pre><code>while(!done){
  
      printf("Thread %d not done yet...\n", m);
      if(m == 0){
          sem_wait(&amp;threadSem0);
      }else if(m == 1){
          sem_wait(&amp;threadSem1);
      }else if(m == 2){
          sem_wait(&amp;threadSem2);
      }
      printNum = baseNum + i;
      printf("Thread %d past waiting, number segment: %d\n", m, printNum);
  
      finishedThreads++;
      if(finishedThreads == 3){
          sem_post(&amp;parentSem);
      }
  }
  </code></pre>
  
  <p>Parent portion:</p>
  
  <pre><code>for(i = 0; i &lt; 4; i++){
      printf("In parent for loop, counter: %d\n", i);
      printf("Parent past wait semaphore\n");
      finishedThreads = 0;
      if(i == 3) done = 1;
      sem_post(&amp;threadSem0);
      sem_post(&amp;threadSem1);
      sem_post(&amp;threadSem2);
      sem_wait(&amp;parentSem);
  }
  for(i = 1; i &lt; 3; i++){
      pthread_join(threads[i], NULL);
  }
  </code></pre>