title: Why am I experiencing a deadlock with pthread_cond_signal and pthread_cond_wait?
body: <p><strong>RESOLVED</strong>
  I am having some problems with synchronization.</p>
  <p>Firstly, the purpose of the file is to create 10 threads, and print the odd numbered ids before the even numbered ids.</p>
  <p>As far as I can tell, my pthread_cond_wait is creating a deadlock. I was able to resolve this deadlock by including the signal below in the threadFunction. <strong>If I do not include this, I am stuck in running the program after all the odd numbers thread ids have been printed.</strong></p>
  <pre><code>        perror(&quot;Unable to pthread_cond_broadcast&quot;);
          exit(EXIT_FAILURE);
        }
  </code></pre>
  <p><strong>Updated Section - Solution</strong></p>
  <pre><code>void *threadFunction(void *threadId) {
      if ((long) threadId % 2 == 0 &amp;&amp; remainingOddThreads &gt; 0) {
          pthread_mutex_lock(&amp;lock);
          if (pthread_cond_wait(&amp;evenCanGo, &amp;lock)) {
              perror(&quot;Error setting a pthread_cond_wait:&quot;);
              exit(EXIT_FAILURE);
          }
          printf(&quot;Hello from thread[%ld]!\n&quot;, (long) threadId);
          pthread_mutex_unlock(&amp;lock);
      } else {
          if (pthread_mutex_lock(&amp;lock)) {
              perror(&quot;Unable to lock mutex:&quot;);
              exit(EXIT_FAILURE);
          }
          printf(&quot;Hello from thread[%ld]!\n&quot;, (long) threadId);
          remainingOddThreads--;
          if (pthread_mutex_unlock(&amp;lock)) {
              perror(&quot;Unable to unlock mutex:&quot;);
              exit(EXIT_FAILURE);
          }
      }
      if (remainingOddThreads == 0) {
          pthread_mutex_lock(&amp;lock);
          if (pthread_cond_signal(&amp;evenCanGo)) {
              perror(&quot;Unable to pthread_cond_broadcast&quot;);
              exit(EXIT_FAILURE);
          }
          pthread_mutex_unlock(&amp;lock);
      }
      return NULL;
  }
  </code></pre>
  <p><strong>This is the OLD code file</strong></p>
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;pthread.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;errno.h&gt;
  
  #define NUMBER_OF_ODD_NUMBERS_FROM_ONE_TO_TEN 5;
  #define NUMBER_OF_THREADS 10
  int remainingOddThreads = NUMBER_OF_ODD_NUMBERS_FROM_ONE_TO_TEN
  pthread_t threads[NUMBER_OF_THREADS];
  pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
  pthread_cond_t evenCanGo = PTHREAD_COND_INITIALIZER;
  
  void *threadFunction(void *threadId) {
      if ((long) threadId % 2 == 0 &amp;&amp; remainingOddThreads &gt; 0) {
          if (pthread_cond_wait(&amp;evenCanGo, &amp;lock)) {
              perror(&quot;Error setting a pthread_cond_wait:&quot;);
              exit(EXIT_FAILURE);
          }
          printf(&quot;Hello from thread[%ld]!\n&quot;, (long) threadId);
      } else {
          if (pthread_mutex_lock(&amp;lock)) {
              perror(&quot;Unable to lock mutex:&quot;);
              exit(EXIT_FAILURE);
          }
          printf(&quot;Hello from thread[%ld]!\n&quot;, (long) threadId);
          remainingOddThreads--;
          if (pthread_mutex_unlock(&amp;lock)) {
              perror(&quot;Unable to unlock mutex:&quot;);
              exit(EXIT_FAILURE);
          }
      }
      if (pthread_cond_signal(&amp;evenCanGo)) {
          perror(&quot;Unable to pthread_cond_broadcast&quot;);
          exit(EXIT_FAILURE);
      }
      return NULL;
  }
  
  
  void createThreads() {
      for (long i = 1; i &lt;= NUMBER_OF_THREADS; i++) {
          int status = pthread_create(&amp;threads[i - 1], NULL, threadFunction, (void *) i);
          if (status) {
              perror(&quot;Failed to create thread&quot;);
              exit(EXIT_FAILURE);
          }
      }
  }
  
  void activateThreads() {
      for (long i = 0; i &lt; NUMBER_OF_THREADS; i++) {
          int status = pthread_join(threads[i], NULL);
          if (status &gt; 0) {
              perror(&quot;Unable to join thread&quot;);
              exit(EXIT_FAILURE);
          }
      }
  }
  
  
  int main() {
      createThreads();
      activateThreads();
  
      if (pthread_cond_destroy(&amp;evenCanGo) != 0) {
          perror(&quot;Unable to destroy pthread_cond_t object:&quot;);
          exit(EXIT_FAILURE);
      }
      pthread_mutex_unlock(&amp;lock);
      if (pthread_mutex_destroy(&amp;lock) != 0) {
          perror(&quot;Unable to destroy pthread_mutex_t object:&quot;);
          printf(&quot;%d&quot;, errno);
          exit(EXIT_FAILURE);
      }
      return 0;
  }
  </code></pre>