title: How to Kill child threads from Parent thread - C
body: <p>I have a little program with few processes, each process contains several threads.</p>
  
  <p>When the parent process receives a specific message it is supposed to kill all its sub threads and destroy all the mutexes. The problem is that some of this threads can be blocked (on I/O command) and thus cannot receive some signal or msg in order to finish their procedures. </p>
  
  <p>I thought about using <code>pthread_cancel</code> and override the <code>cancel routine</code> but the problem is that some mutexes appeared to be locked when the <code>cancel signal</code> is sent, and thus cannot be destroyed.<br>Using <code>pthread_kill</code> did not work either because it kills the whole process and thus cannot be used when I want a clean exit.</p>
  
  <p>How can a process kill its child threads and destroy the mutexes in a clean fashion?</p>
  
  <p>The relevant section of the code looks as something like :</p>
  
  <pre><code>ret = fork()
  if(ret &gt; 0) {
      pthread_t th1, th2;
      my_pthread_create(&amp;th1, threadFunction1, NULL);
      my_pthread_create(&amp;th2, threadFunction2, NULL);
      if(msgrcv(qid1, &amp;msg, MSG_SIZE,0,0) &lt; 0) {
          perror("msgrcv failed...\n");
      } else {
          //print some information from the global variables, terminate 
          //threads and destroy mutexes
      }
  }
  else if(ret&lt;0) {
      perror("fork failed...\n");
  } else {
      ....
  }
  </code></pre>
  
  <p>Any help will be really appreciated! <br>
  Thank you</p>