c ++ pthreads - 尝试锁定互斥锁以进行读取时崩溃

时间:2012-06-14 22:55:46

标签: c++ crash gdb pthreads mutex

班级LocalT有其他班级成员实现read-write-mutex。 Mutex在构造函数中初始化并使用pthread_rwlock_rdlock(&aMutex);来读取锁定。所以,似乎,mutex类都可以。但是当一些LocalT对象锁定他的互斥锁成员进行读取时,程序崩溃了。

CSerialize.cpp:2054行是MUTEX.lock_reading();

Thread 6 (Thread 0x80d4e00 (runnable)):
#0  0x4864f11d in pthread_mutex_lock () from /lib/libpthread.so.2
#1  0x4864b558 in pthread_rwlock_init () from /lib/libpthread.so.2
#2  0x4864b659 in pthread_rwlock_rdlock () from /lib/libpthread.so.2
#3  0x0807ae14 in LocalT::serialize (this=0x80d4e00, outbin=@0x7574736b)
    at CSerialize.cpp:2054

其他两个正在运行的线程:

1)在套接字accept();

2)popen()调用时的下一个可运行线程,似乎是来自管道的执行或read。但不知道什么是__error() ?????

Thread 1 (Thread 0x8614800 (LWP 100343)):
#0  0x4865b8f9 in __error () from /lib/libpthread.so.2
#1  0x4865a15a in pthread_testcancel () from /lib/libpthread.so.2
#2  0x486425bf in read () from /lib/libpthread.so.2
#3  0x08056340 in UT::execute_popen (command=@0x4865e6bc,
    ptr_output=0xbf2f7d30) at Utils.cpp:75

3)所有其他线程都在睡觉。

我不知道为什么它会崩溃?也许有人可以假设或建议?

== EDIT ==

这里是一个系统(?)线程(我没有创建它,但程序总是有+1线程)。总是:

Thread 8 (Thread 0x80d4a00 (LWP 100051)):
#0  0x4865a79b in pthread_testcancel () from /lib/libpthread.so.2
#1  0x48652412 in pthread_mutexattr_init () from /lib/libpthread.so.2
#2  0x489fd450 in ?? ()

== EDIT2 - bt按要求==

(gdb) bt
#0  0x4865a7db in pthread_testcancel () from /lib/libpthread.so.2
#1  0x48652412 in pthread_mutexattr_init () from /lib/libpthread.so.2
#2  0x489fd450 in ?? ()
奇怪的是......为什么?? ()

== EDIT3 - 加载核心时==

Program terminated with signal 11, Segmentation fault.
[skiped]
#0  0x4865a7db in pthread_testcancel () from /lib/libpthread.so.2
[New Thread 0x8614800 (LWP 100343)]
[New Thread 0x8614600 (sleeping)]
[New Thread 0x8614400 (sleeping)]
[New Thread 0x8614200 (sleeping)]
[New Thread 0x8614000 (sleeping)]
[New Thread 0x80d4e00 (runnable)]
[New Thread 0x80d4c00 (sleeping)]
[New Thread 0x80d4a00 (LWP 100051)]
[New Thread 0x80d4000 (runnable)]
[New LWP 100802]

(gdb) info thread
* 10 LWP 100802  0x4865a7db in pthread_testcancel () from /lib/libpthread.so.2
  9 Thread 0x80d4000 (runnable)  0x486d7bd3 in accept () from /lib/libc.so.6 -- MAIN() THREAD
  8 Thread 0x80d4a00 (LWP 100051)  0x4865a79b in pthread_testcancel ()
   from /lib/libpthread.so.2 ( UNIDENTIFIED THREAD system()? ) 
  7 Thread 0x80d4c00 (sleeping)  0x48651cb6 in pthread_mutexattr_init ()
   from /lib/libpthread.so.2 (SIGNAL PROCESSOR THREAD)
  6 Thread 0x80d4e00 (runnable)  0x4864f11d in pthread_mutex_lock ()
   from /lib/libpthread.so.2 (MAINTENANCE THREAD)
  5 Thread 0x8614000 (sleeping)  0x48651cb6 in pthread_mutexattr_init ()
   from /lib/libpthread.so.2 (other mutex cond_wait - worker 1)
  4 Thread 0x8614200 (sleeping)  0x48651cb6 in pthread_mutexattr_init ()
   from /lib/libpthread.so.2 (other mutex cond_wait - worker 2 )
  3 Thread 0x8614400 (sleeping)  0x48651cb6 in pthread_mutexattr_init ()
   from /lib/libpthread.so.2 (other mutex cond_wait - worker 3 )
  2 Thread 0x8614600 (sleeping)  0x48651cb6 in pthread_mutexattr_init ()
   from /lib/libpthread.so.2 (other mutex cond_wait - worker 4)
  1 Thread 0x8614800 (LWP 100343)  0x4865b8f9 in __error ()
   from /lib/libpthread.so.2 ( popen() thread see below)

我创建了:1个维护线程(序列化),1个popen()线程,4个worker,1个main,1个信号线程= 8个线程....

1 个答案:

答案 0 :(得分:1)

您称为系统线程的线程实际上是您的程序main thread

其次,到目前为止您分享的信息,看起来您正在获取互斥锁但从未发布它。这导致一个不稳定的状态(一些参数有错误的值)导致崩溃。我相信你也会观察间歇性的悬挂。

崩溃后你能分享backtrace吗?