title: multi threading inside threads in c program
body: <p>I am debugging a c code which creates 3 threads.These 3 threads wait(using pthread_cond_wait) and when my program signals them(using pthread_cond_signal) they create 4 threads each. Now when these 4 threads starts running, a <strong>segmentation fault</strong> occurs.</p>
  
  <p>my gdb output:</p>
  
  <pre><code>[New Thread 0x7fffd57fa700 (LWP 707)]
  
  Program received signal SIGSEGV, Segmentation fault.
  [Switching to Thread 0x7fffd77fe700 (LWP 701)]
  0x00007ffff79b591f in pthread_cond_wait@@GLIBC_2.3.2 () from /usr/lib/libpthread.so.0
  
  
  (gdb) info threads 
    Id   Target Id         Frame 
  
    18   Thread 0x7fffd57fa700 (LWP 707) "scan_mt_skip_de" 0x00007ffff79b80db in __lll_lock_wait_private () from /usr/lib/libpthread.so.0
  
    17   Thread 0x7fffd5ffb700 (LWP 704) "scan_mt_skip_de" 0x00007ffff79b5954 in pthread_cond_wait@@GLIBC_2.3.2 () from /usr/lib/libpthread.so.0
  
    16   Thread 0x7fffd67fc700 (LWP 703) "scan_mt_skip_de" 0x00007ffff79b5954 in pthread_cond_wait@@GLIBC_2.3.2 () from /usr/lib/libpthread.so.0
  
    15   Thread 0x7fffd6ffd700 (LWP 702) "scan_mt_skip_de" 0x00007ffff79b5954 in pthread_cond_wait@@GLIBC_2.3.2 () from /usr/lib/libpthread.so.0
  
  * 14   Thread 0x7fffd77fe700 (LWP 701) "scan_mt_skip_de" 0x00007ffff79b591f in pthread_cond_wait@@GLIBC_2.3.2 () from /usr/lib/libpthread.so.0
  
    13   Thread 0x7fffd7fff700 (LWP 578) "scan_mt_skip_de" 0x00007ffff79b5954 in pthread_cond_wait@@GLIBC_2.3.2 () from /usr/lib/libpthread.so.0
  
    12   Thread 0x7fffe0e6f700 (LWP 577) "scan_mt_skip_de" 0x00007ffff62aed37 in mprotect () from /usr/lib/libc.so.6
  
    11   Thread 0x7fffe1b21700 (LWP 576) "scan_mt_skip_de" 0x00007ffff79b5954 in pthread_cond_wait@@GLIBC_2.3.2 () from /usr/lib/libpthread.so.0
  
    10   Thread 0x7fffe2c84700 (LWP 574) "scan_mt_skip_de" 0x00007ffff79b83ed in read () from /usr/lib/libpthread.so.0
  
    9    Thread 0x7ffff157b700 (LWP 32268) "scan_mt_skip_de" 0x00007ffff79b5954 in pthread_cond_wait@@GLIBC_2.3.2 () from /usr/lib/libpthread.so.0
  
    8    Thread 0x7ffff1d7c700 (LWP 32265) "scan_mt_skip_de" 0x00007ffff79b5954 in pthread_cond_wait@@GLIBC_2.3.2 () from /usr/lib/libpthread.so.0
  
    7    Thread 0x7ffff257d700 (LWP 32244) "scan_mt_skip_de" 0x00007ffff62a9fad in poll () from /usr/lib/libc.so.6
  
    6    Thread 0x7ffff2d7e700 (LWP 32233) "scan_mt_skip_de" 0x00007ffff79b5954 in pthread_cond_wait@@GLIBC_2.3.2 () from /usr/lib/libpthread.so.0
  
    5    Thread 0x7ffff357f700 (LWP 32231) "scan_mt_skip_de" 0x00007ffff79b5954 in pthread_cond_wait@@GLIBC_2.3.2 () from /usr/lib/libpthread.so.0
  
    4    Thread 0x7ffff41c3700 (LWP 32229) "scan_mt_skip_de" 0x00007ffff62a9fad in poll () from /usr/lib/libc.so.6
  
    3    Thread 0x7ffff49c4700 (LWP 32228) "scan_mt_skip_de" 0x00007ffff79b5954 in pthread_cond_wait@@GLIBC_2.3.2 () from /usr/lib/libpthread.so.0
  
    2    Thread 0x7ffff51e6700 (LWP 32227) "scan_mt_skip_de" 0x00007ffff79b5d01 in pthread_cond_timedwait@@GLIBC_2.3.2 () from /usr/lib/libpthread.so.0
  
    1    Thread 0x7ffff7fe1780 (LWP 32223) "scan_mt_skip_de" 0x00007ffff62f9833 in __memcpy_ssse3 () from /usr/lib/libc.so.6
  
  
  (gdb) bt
  
  0  0x00007ffff79b591f in pthread_cond_wait@@GLIBC_2.3.2 () from /usr/lib/libpthread.so.0
  
  1  0x000000000040385d in myDmtxDecodeThread ()
  
  2  0x00007ffff79b1e0f in start_thread () from /usr/lib/libpthread.so.0
  
  3  0x00007ffff62b2efd in clone () from /usr/lib/libc.so.6
  </code></pre>
  
  <p>In my code, when I create 3 threads, I lock a mutex variable and then wait. But in each 4 threads, I just use a mutex variable and wait on it.</p>
  
  <p>Part of the code:</p>
  
  <p>in 3 threads:</p>
  
  <pre><code>{
      pthread_mutex_lock(tdata-&gt; wait_mutex);
      pthread_cond_wait(tdata-&gt; wait_cv, tdata-&gt; wait_mutex);
  }
  </code></pre>
  
  <p>in 4 threads:</p>
  
  <pre><code>{
      pthread_cond_wait(thdata-&gt;wait_cv_th,thdata-&gt;wait_mutex_th);
  }
  </code></pre>