c ++ pthreads - 尝试锁定互斥读取时崩溃 [英] c++ pthreads - crash while trying to lock mutex for reading

查看:356
本文介绍了c ++ pthreads - 尝试锁定互斥读取时崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Class LocalT have member of other class that realized read-write-mutex. Mutex initialized at constructor and use pthread_rwlock_rdlock(&aMutex); for reading lock. So, seems, its all ok with mutex class. But program crashed when some LocalT object lock his mutex member for reading.

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

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

Other two running threads:

1)在套接字 accept();

2)下一个runnable线程< c $ c> popen()调用,似乎它的execute或从管道读取。但不知道 __ error()

2) next runnable thread at popen() call, seems its execute or read from pipe. But does not know what is __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)所有其他线程处于休眠状态。

3) all other thread sleeping.

我没有想法为什么它崩溃?也许有人可以假设某事或建议?

I have no ideas why its crashed? Maybe somebody can assume something or suggest?

== EDIT ==

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

and here is one system(?) thread ( i does not create it, but program always have +1 thread). It always:

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 as requested ==

(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() thread,4 workers,1 main,1 signal thread = 8 threads ....

I created: 1 maintenance thread (serializing), 1 popen() thread, 4 workers, 1 main, 1 signal thread = 8 threads....

推荐答案

因为系统线程实际上是你程序的主线程

the thread that you are referring to as system thread is actually your program's main thread.

其次,您正在获取互斥体,但不会释放它。导致导致崩溃的不稳定状态(一些参数具有错误的值)。我相信你也会观察到间歇性的挂。

Secondly with information shared by you so far, it looks like you are acquiring the mutex but never releasing it. that leads to an unstable state (some parameters having wrong values) which leads to a crash. I am sure you will also be observing an intermittent hang.

可以在崩溃时共享 backtrace 吗?

could you share the backtrace when it crashes ?

这篇关于c ++ pthreads - 尝试锁定互斥读取时崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆