multithreading - multi threading inside threads in c program -


i debugging c code creates 3 threads.these 3 threads wait(using pthread_cond_wait) , when program signals them(using pthread_cond_signal) create 4 threads each. when these 4 threads starts running, segmentation fault occurs.

my gdb output:

[new thread 0x7fffd57fa700 (lwp 707)]  program received signal sigsegv, segmentation fault. [switching thread 0x7fffd77fe700 (lwp 701)] 0x00007ffff79b591f in pthread_cond_wait@@glibc_2.3.2 () /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 () /usr/lib/libpthread.so.0    17   thread 0x7fffd5ffb700 (lwp 704) "scan_mt_skip_de" 0x00007ffff79b5954 in pthread_cond_wait@@glibc_2.3.2 () /usr/lib/libpthread.so.0    16   thread 0x7fffd67fc700 (lwp 703) "scan_mt_skip_de" 0x00007ffff79b5954 in pthread_cond_wait@@glibc_2.3.2 () /usr/lib/libpthread.so.0    15   thread 0x7fffd6ffd700 (lwp 702) "scan_mt_skip_de" 0x00007ffff79b5954 in pthread_cond_wait@@glibc_2.3.2 () /usr/lib/libpthread.so.0  * 14   thread 0x7fffd77fe700 (lwp 701) "scan_mt_skip_de" 0x00007ffff79b591f in pthread_cond_wait@@glibc_2.3.2 () /usr/lib/libpthread.so.0    13   thread 0x7fffd7fff700 (lwp 578) "scan_mt_skip_de" 0x00007ffff79b5954 in pthread_cond_wait@@glibc_2.3.2 () /usr/lib/libpthread.so.0    12   thread 0x7fffe0e6f700 (lwp 577) "scan_mt_skip_de" 0x00007ffff62aed37 in mprotect () /usr/lib/libc.so.6    11   thread 0x7fffe1b21700 (lwp 576) "scan_mt_skip_de" 0x00007ffff79b5954 in pthread_cond_wait@@glibc_2.3.2 () /usr/lib/libpthread.so.0    10   thread 0x7fffe2c84700 (lwp 574) "scan_mt_skip_de" 0x00007ffff79b83ed in read () /usr/lib/libpthread.so.0    9    thread 0x7ffff157b700 (lwp 32268) "scan_mt_skip_de" 0x00007ffff79b5954 in pthread_cond_wait@@glibc_2.3.2 () /usr/lib/libpthread.so.0    8    thread 0x7ffff1d7c700 (lwp 32265) "scan_mt_skip_de" 0x00007ffff79b5954 in pthread_cond_wait@@glibc_2.3.2 () /usr/lib/libpthread.so.0    7    thread 0x7ffff257d700 (lwp 32244) "scan_mt_skip_de" 0x00007ffff62a9fad in poll () /usr/lib/libc.so.6    6    thread 0x7ffff2d7e700 (lwp 32233) "scan_mt_skip_de" 0x00007ffff79b5954 in pthread_cond_wait@@glibc_2.3.2 () /usr/lib/libpthread.so.0    5    thread 0x7ffff357f700 (lwp 32231) "scan_mt_skip_de" 0x00007ffff79b5954 in pthread_cond_wait@@glibc_2.3.2 () /usr/lib/libpthread.so.0    4    thread 0x7ffff41c3700 (lwp 32229) "scan_mt_skip_de" 0x00007ffff62a9fad in poll () /usr/lib/libc.so.6    3    thread 0x7ffff49c4700 (lwp 32228) "scan_mt_skip_de" 0x00007ffff79b5954 in pthread_cond_wait@@glibc_2.3.2 () /usr/lib/libpthread.so.0    2    thread 0x7ffff51e6700 (lwp 32227) "scan_mt_skip_de" 0x00007ffff79b5d01 in pthread_cond_timedwait@@glibc_2.3.2 () /usr/lib/libpthread.so.0    1    thread 0x7ffff7fe1780 (lwp 32223) "scan_mt_skip_de" 0x00007ffff62f9833 in __memcpy_ssse3 () /usr/lib/libc.so.6   (gdb) bt  0  0x00007ffff79b591f in pthread_cond_wait@@glibc_2.3.2 () /usr/lib/libpthread.so.0  1  0x000000000040385d in mydmtxdecodethread ()  2  0x00007ffff79b1e0f in start_thread () /usr/lib/libpthread.so.0  3  0x00007ffff62b2efd in clone () /usr/lib/libc.so.6 

in code, when create 3 threads, lock mutex variable , wait. in each 4 threads, use mutex variable , wait on it.

part of 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); } 

but, in each 4 threads, use mutex variable , wait on it.

well, wrong. must lock mutex before waiting (which release lock while waiting on conditional, acquire again before returning wait, blocking if locked else). and, since pthread_cond_wait() returns mutex locked, must unlock it.


Comments

Popular posts from this blog

ruby - Trying to change last to "x"s to 23 -

jquery - Clone last and append item to closest class -

c - Unrecognised emulation mode: elf_i386 on MinGW32 -