Gaurav Kumar
Gaurav Kumar

Reputation: 153

multi threading inside threads in c program

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 segmentation fault occurs.

my gdb output:

[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

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.

Part of the code:

in 3 threads:

{
    pthread_mutex_lock(tdata-> wait_mutex);
    pthread_cond_wait(tdata-> wait_cv, tdata-> wait_mutex);
}

in 4 threads:

{
    pthread_cond_wait(thdata->wait_cv_th,thdata->wait_mutex_th);
}

Upvotes: 2

Views: 2552

Answers (1)

hyde
hyde

Reputation: 62906

But, in each 4 threads, I just use a mutex variable and wait on it.

Well, this is wrong. You must lock the mutex before waiting (which will release lock while waiting on conditional, then acquire it again before returning from wait, blocking if it is locked by somebody else). And, since pthread_cond_wait() returns with mutex locked, you must also unlock it.

Upvotes: 4

Related Questions