title: deadlock detection in multithreaded application
body: <p>I am using multithreaded C++ application using posix threads i.e. threads get created through pthread_create. There are several semaphores that control the synchronization. Semaphores are primarily of two kinds: </p>
  
  <ol>
  <li><p>mutex semaphores - Critical section code under mutual exclusion between pthread_mutex_lock and pthread_mutex_unlock calls (done by same thread)</p></li>
  <li><p>Synchronization semaphores - One thread waits calling sem_wait for some tasks to be done, other thread upon completion of the task, signals through sem_post.</p></li>
  </ol>
  
  <p>The application seem to unresponsive while running for a longer time during processing of large volume of data.</p>
  
  <p>Two possibilities that come to my mind:  </p>
  
  <ol>
  <li>There is a deadlock, two threads are waiting on one another (cyclic waiting)</li>
  <li>The parent process (telnet session) gets timed out resulting in sending a SIGHUP signal to the child process.</li>
  </ol>
  
  <p><strong>Questions</strong>:  </p>
  
  <ol>
  <li>Can I detect deadlock through log? For example, on every wait/post, before sem_wait, log as going to wait, after sem_wait acquires the lock, waiting over and after sem_post (which is non blocking anyway), log as signal/post for waiting process -- then if the program appears to hang, check the log to detect any cyclic waiting. Is there any better way suggested?</li>
  <li>Run application using nohup so that SIGHUP can be ignored.</li>
  </ol>
  
  <p>Any other suggestions?</p>