title: How to use pthread_mutex_trylock?
body: <p>Using trylock:</p>
  
  <pre><code>FILE           *fp;
  pthread_mutex_t demoMutex;
  
  void * printHello (void* threadId)
  {
      pthread_mutex_trylock (&amp;demoMutex);
  
      pthread_t      writeToFile = pthread_self ();
      unsigned short iterate;
      for (iterate = 0; iterate &lt; 10000; iterate++)
      {
          fprintf (fp, " %d ",  iterate,         4);
          fprintf (fp, " %lu ", writeToFile, sizeof (pthread_t));
          fprintf (fp, "\n",     writeToFile, 1);
      }
  
      pthread_mutex_unlock (&amp;demoMutex);
      pthread_exit (NULL);
  }
  </code></pre>
  
  <p>and then main ():</p>
  
  <pre><code>int main ()
  {
      pthread_t        arrayOfThreadId [5];
      int                  returnValue;
      unsigned int iterate;
  
      fp = fopen ("xyz", "w");
      pthread_mutex_init (&amp;demoMutex, NULL);
  
      for (iterate = 0; iterate &lt; 5; iterate++)
      {
          if (returnValue = pthread_create (&amp;arrayOfThreadId [iterate],
                                      NULL,
                                      printHello,
                                      (void*) &amp;arrayOfThreadId [iterate]) != 0)
          {
              printf ("\nerror: pthread_create failed with error number %d", returnValue);
          }
      }
  
      for (iterate = 0; iterate &lt; 5; iterate++)
          pthread_join (arrayOfThreadId [iterate], NULL);
  
      return 0;
  }
  </code></pre>
  
  <p>Here the output first prints some of the first thread and then the rest, and then again the first. The lock isn't working. If I replace the same with <code>pthread_mutex_lock</code> every thing gets shown very sequentially!</p>
  
  <p>What's the ridiculous mistake here?</p>