gpt4 book ai didi

c - C程序中线程内的多线程

转载 作者:行者123 更新时间:2023-12-03 12:52:38 24 4
gpt4 key购买 nike

我正在调试一个创建3个线程的c代码。这3个线程正在等待(使用pthread_cond_wait),当我的程序向它们发出信号(使用pthread_cond_signal)时,它们每个创建了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);
}

最佳答案

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



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

关于c - C程序中线程内的多线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15201628/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com