【发布时间】:2015-10-05 22:22:14
【问题描述】:
我的应用程序停止时会崩溃。 Gdb 显示以下堆栈(应用程序是使用 -g -O0 构建的):
(gdb) bt
#0 0x0000000000000000 in ?? ()
#1 0x00007f254ea99700 in ?? ()
#2 0x0000000000000000 in ?? ()
简短的调查表明,在停止线程时会发生崩溃,该线程的启动方式与应用程序中的许多其他线程相同:
// mListener is std::thread and member of class UA
std::thread thr(&UA::run, this);
mListener = std::move(thr);
然后我在停止之前在 app 上运行了 gdb,并看到了导致崩溃的线程堆栈和其他线程之间的区别。 所有线程看起来像:
...
#8 0x000000000043a70a in std::thread::_Impl<std::_Bind_simple<std::_Mem_fn<void (UI::Keyboard::*)()> (UI::Keyboard*)> >::_M_run() (this=0xa88fd0)
at /usr/include/c++/4.9/thread:115
#9 0x00007fb6055c3970 in ?? () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#10 0x00007fb6083ff0a4 in start_thread (arg=0x7fb604042700) at pthread_create.c:309
#11 0x00007fb604d3304d in clone () at ../sysdeps/unix/sysv/linux/x86_64/clone.S:111
但“错误”的线程总是看起来不同:
#0 sem_wait () at ../nptl/sysdeps/unix/sysv/linux/x86_64/sem_wait.S:85
#1 0x000000000043317d in Semaphore::wait (this=0x7fb5fc0009e8) at /home/vadius/workspace/iPhone/core/src/Core/env/Semaphore.h:28
#2 0x0000000000432564 in SIP::UA::run (this=0x7fb5fc000980) at /home/vadius/workspace/iPhone/core/src/SIP/UA.cpp:132
#3 0x0000000000000000 in ?? ()
我假设当线程从工作方法 (SIP::UA::run) 退出时,它会转到放置在 nullptr 中的代码。
我的问题是:
1. 我是对的,而一堆“坏”线程是错的吗?
2. 这种行为可能是什么原因以及如何避免?
Debian jessie x64 /
海合会 4.9 /
编译标志:set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -DDEBUG -g -O0")
【问题讨论】:
-
我觉得不对。可能是您的代码损坏了它。
标签: c++ linux multithreading c++11