c程序中的线程内部多线程

时间:2013-03-04 12:28:44

标签: c multithreading gdb

我正在调试一个创建3个线程的c代码。这3个线程等待(使用pthread_cond_wait),当我的程序发出信号时(使用pthread_cond_signal),它们分别创建4个线程。现在,当这4个线程开始运行时,会出现分段错误

我的gdb输出:

[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

在我的代码中,当我创建3个线程时,我锁定一个互斥变量然后等待。但是在每4个线程中,我只使用一个互斥变量并等待它。

部分代码:

3个主题:

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

4个主题:

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

1 个答案:

答案 0 :(得分:4)

  

但是,在每4个线程中,我只使用互斥变量并等待它。

嗯,这是错的。您必须在等待之前锁定互斥锁(在等待条件时释放锁定,然后在等待返回之前再次获取锁定,如果被其他人锁定则阻止它)。并且,由于pthread_cond_wait()在锁定互斥锁时返回,因此您还必须将其解锁。