title: pthread_cond_wait() doesn't make CPU sleep?
body: <p>All </p>
  
  <p>I have a question about pthread_cond_wait().
  In short, I create two POSIX thread in a process, 
  If I execute the following code, why is cpu utilization full?</p>
  
  <p>I take experiments on it, if I remove comment mark before bool isNodeConnect3,
  the program seems to be no probelm, CPU utilization is almost 0%, in other words, 
  theads will go to sleep and don't spend CPU resource, that's what I want.</p>
  
  <p>Is it a data algnment probelm?
  maybe, but I don't think so, because I bracket my struct by "#pragma pack(push,1) ... #pragma (pop)"
  Could you give me suggestion??</p>
  
  <p>Environment
  Host OS is win7/intel 64 bit, guest OS is ubuntu 10.04LTS
  Give "number of processor cores:4" to guest OS
  The following is my test code, you can build and run it by<br>
  gcc -o program1 program1.c -pthread &amp;&amp; ./program1
  Get CPU utilization is 25%. Result depends on your setting.</p>
  
  <p>Thanks a lot.</p>
  
  <p>Code</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;fcntl.h&gt;
  #include &lt;string.h&gt;
  #include &lt;pthread.h&gt;
  #include &lt;sys/ipc.h&gt;
  #include &lt;sys/msg.h&gt;
  #include &lt;errno.h&gt;
  #include &lt;stdbool.h&gt;
  
  
  #pragma pack(push,1)
  struct BUFF_TX{
      pthread_mutex_t mutex_lock;
      pthread_cond_t more;
  };
  
  struct AtreeNode{
      struct BUFF_TX buff_tx;
  
      bool isNodeConnect;                   
      bool isNodeConnect1;                  
      bool isNodeConnect2;                  
  //  bool isNodeConnect3;  // important           
  
      pthread_t thrd_tx;          
  
  };
  
  
  struct AtreeNode treeNode[2];
  int tmp[2];  
  
  #pragma (pop)
  
  
  void Thread_TX(int *nodeIdx)
  {
      int idx    = *nodeIdx;
  
      while(1)
      {
  
          printf("Thread %d enter mutex lock...\n", idx);
          pthread_mutex_lock(&amp;treeNode[idx].buff_tx.mutex_lock);       
          while(1)
          {
              if(idx==0)
              {
                  printf("idx==0 wait...\n");
                  pthread_cond_wait(&amp;(treeNode[0].buff_tx.more), &amp;treeNode[idx].buff_tx.mutex_lock);
              }
              else if(idx==1)
              {
                  printf("idx==1 wait...\n");
                  pthread_cond_wait(&amp;(treeNode[1].buff_tx.more), &amp;treeNode[idx].buff_tx.mutex_lock);
              }
              else
                  printf("err\n");
  
          }
          pthread_mutex_unlock(&amp;treeNode[idx].buff_tx.mutex_lock);            
          printf("Thread %d leave mutex lock...\n", idx);
  
      }
  
  }
  
  
  
  
  int main(int argc, char *argv[])
  {
      int i;
      int ret;
  
      tmp[0] = 0;
      tmp[1] = 1;
  
  
      for(i=0; i&lt;2; i++)
      {
          if(pthread_cond_init(&amp;treeNode[i].buff_tx.more, NULL) != 0)
          {
              printf("cond %d init fail.\n", i);
              exit(EXIT_FAILURE);
          }
  
          if(pthread_mutex_init(&amp;treeNode[i].buff_tx.mutex_lock, NULL) != 0)
          {
              printf("mutex lock %d init fail.\n", i);
              exit(EXIT_FAILURE);
          }
      }
  
      for(i=0; i&lt;2; i++)
      {   
          ret = pthread_create(&amp;treeNode[i].thrd_tx, NULL, (void *)Thread_TX, (void *)(&amp;tmp[i]));
          if(ret) 
          {
              printf("pthread_create thrd_tx %d err\n", i);
              return false;
          }
      }
  
      pthread_join(treeNode[0].thrd_tx, NULL);
      pthread_join(treeNode[1].thrd_tx, NULL);
  
      exit(EXIT_SUCCESS);
  }
  </code></pre>