2

我正在调试创建 3 个线程的 ac 代码。这 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);
}
4

1 回答 1

4

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

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

于 2013-03-04T12:41:50.293 回答