title: C: Using pthreads and program is not exiting a for loop
body: <p>I am simulating a device that will receive bytes of data and write to a register when the clock has a falling edge. I've created a seperate pthread that will account for shifting the clock between high and low. </p>
  
  <p>Now my issue is that in the main thread I have the console ask the user to type a hex value to write to the "device" and it then it calls a SendCommand(uint32_t addr, uint16_t data) function to do that. Since it is supposed to send one bit at a time per clock cycle, I have it in a for loop. Here is the send command function:</p>
  
  <pre><code>void SendCmd(uint32_t loadAddr, uint16_t data)
  {
      dest = &amp;loadAddr;
      int i;
      printf("%d\n\n",status);
      for(i=15; i&gt;=0; --i) {
          pthread_cond_wait(&amp;rising_edge, &amp;data_lock);
          *dest = (*dest &lt;&lt; 1)|(data &amp; 0x8000) &gt;&gt; 15;
                 //     ^^^^^^^^ get MSB
                 //               ^^^^^ move the MSB down to the LSB, assuming the
                 //                     peripheral wants the bit in the LSB.
          pthread_cond_wait(&amp;falling_edge, &amp;data_lock);
          data &lt;&lt;= 1;
          printf("%d\ti=%d\n",*dest,i);
      }
      pthread_mutex_unlock(&amp;data_lock);
      status = ENABLED_IDLE;
  }
  </code></pre>
  
  <p>When i=0, the loop should exit and return back to the main function where it will ask the user to enter another hex value. But it doesnt, and I can't seem to find why it would be stuck before exiting the loop.</p>
  
  <p>EDIT: The freeze is actually occurring before the function ever executes:</p>
  
  <pre><code>int main()
  {
      State status = DISABLED;
      Clock clk = LOW;
      int exit_flag = 0;
      char Command[20];
      pthread_t clk_t;
      pthread_cond_init(&amp;en_sig, NULL);
      pthread_cond_init(&amp;rising_edge, NULL);
      pthread_cond_init(&amp;falling_edge, NULL);
      pthread_mutex_init(&amp;clk_lock, NULL);
      pthread_mutex_init(&amp;data_lock, NULL);
      pthread_create(&amp;clk_t, NULL, simClk, NULL);
      while(!exit_flag)
      {
          if(status != ENABLED_ACTIVE)
          {
              fputs("(enable/disable/write [addr] [word_0] [word_1*]/quit): ", stdout);
              fflush(stdout);
              if ( fgets(Command, sizeof Command, stdin) != NULL )
              {
                  char *newline = strchr(Command, '\n');
                  if ( newline != NULL )
                  {
                      *newline = '\0';
                  }
              }
  
              if(strcasecmp(Command,"quit")==0)
              {
                  printf("\nShutting Down!\n\n");
                  pthread_cancel(&amp;clk_t);
                  pthread_mutex_destroy(&amp;clk_lock);
                  exit_flag = ~exit_flag;
                  continue;
              }
  
              if(strcasecmp(Command,"enable")==0)
              {
                  if(status != DISABLED)
                  {
                      printf("\nSynthesizer is already enabled!\n\n");
                      continue;
                  }
                  status = ENABLED_IDLE;
                  nanosleep(&amp;ecsWait, NULL);
                  printf("\nEnabled!\n\n");
                  pthread_cond_signal(&amp;en_sig);
                  continue;
              }
  
              if(strcasecmp(Command,"disable")==0)
              {
                  status = DISABLED;
                 // pthread_mutex_lock(&amp;clk_lock);
                  printf("\nDisabled!\n\n");
                  continue;
              }
  
              if(strcasecmp(Command,"w")==0)
              {
                  if(status != ENABLED_IDLE)
                  {
                      printf("\nSynthesizer is not enabled!\n\n");
                      continue;
                  }
                  printf("\nWriting!\n\n");
                  status = ENABLED_ACTIVE;      //FREEZE OCCURS HERE!
                  SendCmd(NREGISTER_ADDR,0xF13F);
                  continue;
              }
  
              printf("Invalid command!\n");
          }
     }
      return 0;
  
  }
  </code></pre>