title: C pthread join an ended thread
body: <p>I'm building 2 programs (client/server) that communicate through FIFOs. Both programs have threads. When the Client's thread ends it doesn't get joined, and main hangs.</p>
  
  <p>The programs do the following:</p>
  
  <p>Server:</p>
  
  <ol>
  <li>main: reads from FIFO1 </li>
  <li>main: create thread to process request. </li>
  <li>main: goto 1 </li>
  <li>thread: processes request </li>
  <li>thread: sends response to FIFO2 </li>
  <li>thread: exit</li>
  </ol>
  
  <p>Client:</p>
  
  <ol>
  <li>main: spawn thread</li>
  <li>thread: reads X responses to client from FIFO2</li>
  <li>thread: exit</li>
  <li>main: sends X requests to FIFO1</li>
  <li>main: wait thread to exit</li>
  <li>main: check responses</li>
  </ol>
  
  <p>The server works well and all threads are joined accordingly.</p>
  
  <p>The client fails in step 5. Using </p>
  
  <pre><code>pthread_join(&amp;reader,NULL);
  </code></pre>
  
  <p>hangs main forever. I've checked, and the thread already ended.</p>
  
  <p>Using   </p>
  
  <pre><code>pthread_tryjoin_np(&amp;reader,NULL);
  </code></pre>
  
  <p>I get </p>
  
  <blockquote>
    <p>errorcode=16 </p>
  </blockquote>
  
  <p>strerror gives </p>
  
  <blockquote>
    <p>Device or resource busy</p>
  </blockquote>
  
  <p>Creating the thread with:</p>
  
  <pre><code>pthread_attr_init(&amp;attr);
  pthread_attr_setdetachstate(&amp;attr,PTHREAD_CREATE_JOINABLE);
  pthread_create(&amp;reader,&amp;attr,trataResp,NULL);
  </code></pre>
  
  <p>Or with:
      pthread_create(&amp;reader,NULL,trataResp,NULL);</p>
  
  <p>doesn't produce any change.</p>
  
  <p>How can I resolve this?</p>