如何理解pthread_cancel导致"terminate called without an active exception"?
How to understand pthread_cancel cause "terminate called without an active exception"?
i修改并从本文中测试程序:
#include <unistd.h>
#include <pthread.h>
#include <iostream>
using namespace std;
struct Sleepy
{
~Sleepy()
{
cerr<<"...zZzZz..."<<endl;
sleep(999);
}
};
void* sleepyThread(void*)
{
Sleepy f;
cerr<<"Fall asleep...n";
}
int main()
{
pthread_t thread;
int id=pthread_create(&thread,NULL,&sleepyThread,NULL);
sleep(1); //Give the new thread time to get to the sleeping part...
cerr<<"lets try to cancel it..."<<endl;
pthread_cancel(thread);
pthread_join(thread,NULL);
cerr<<"All is done now..."<<endl;
}
运行它将导致核心转储:
......
terminate called without an active exception
Aborted (core dumped)
堆栈回溯是这样的:
(gdb) bt
#0 0x00007f30325528c0 in raise () from /usr/lib/libc.so.6
#1 0x00007f3032553f72 in abort () from /usr/lib/libc.so.6
#2 0x00007f3032e80035 in __gnu_cxx::__verbose_terminate_handler ()
at /build/gcc/src/gcc/libstdc++-v3/libsupc++/vterminate.cc:95
#3 0x00007f3032e7dc46 in __cxxabiv1::__terminate (handler=<optimized out>)
at /build/gcc/src/gcc/libstdc++-v3/libsupc++/eh_terminate.cc:47
#4 0x00007f3032e7dc91 in std::terminate () at /build/gcc/src/gcc/libstdc++-v3/libsupc++/eh_terminate.cc:57
#5 0x00007f3032e7d7e0 in __cxxabiv1::__gxx_personality_v0 (version=<optimized out>, actions=<optimized out>,
exception_class=0, ue_header=<optimized out>, context=<optimized out>)
at /build/gcc/src/gcc/libstdc++-v3/libsupc++/eh_personality.cc:670
#6 0x00007f30328d4fb5 in _Unwind_ForcedUnwind_Phase2 (exc=exc@entry=0x7f303251ed70,
context=context@entry=0x7f303251d750) at /build/gcc/src/gcc/libgcc/unwind.inc:175
#7 0x00007f30328d5575 in _Unwind_ForcedUnwind (exc=0x7f303251ed70, stop=0x7f30331861b0 <unwind_stop>,
stop_argument=<optimized out>) at /build/gcc/src/gcc/libgcc/unwind.inc:207
#8 0x00007f3033186351 in __pthread_unwind () from /usr/lib/libpthread.so.0
#9 0x00007f303317b7d2 in sigcancel_handler () from /usr/lib/libpthread.so.0
#10 <signal handler called>
#11 0x00007f30325dabcd in nanosleep () from /usr/lib/libc.so.6
#12 0x00007f30325dab0a in sleep () from /usr/lib/libc.so.6
#13 0x0000560ce9b04d92 in Sleepy::~Sleepy (this=0x7f303251dee7, __in_chrg=<optimized out>) at sleepy.cpp:10
#14 0x0000560ce9b04bf5 in sleepyThread () at sleepy.cpp:16
#15 0x00007f303317d049 in start_thread () from /usr/lib/libpthread.so.0
#16 0x00007f303260cf0f in clone () from /usr/lib/libc.so.6
我无法完全理解帖子中写的原因。根据我的理解,它应该在__pthread_unwind()
中,导致stack unwinding
会使局部变量Sleepy f
回收,这将再次触发Sleepy f
的destructor再次调用。这似乎是不合理的。
我的理解正确吗?谁能给出更多详细的解释?
不确定它是否适用于您的案件,但是在C 11中,PTHREAD_CANCEL无法正常工作。
参见例如
https://gcc.gnu.org/ml/gcc-help/2015-08/msg00040.html
相关文章:
- dynamic_cast without RTTI
- N-API include an .so or dll
- C++/SDL "initial value of reference to a non-const must be an lvalue"
- Qt - QQuickWidget setSource without freezing UI
- "in-situ without memory allocation" 字符串的愚蠢实现意味着什么?
- 如何理解"abstract-declarator containing an ellipsis shall only be used in a parameter-declaration"
- 如何在数据库程序中添加"reduce the quantity of an item"功能?
- C++线程"Call to non-static member function without an object argument"
- Atom gpp编译器给出"'C:UsersadminUser' is not recognized as an internal or..."错误
- 尝试在类中编译内核,出现错误"__init__() got an unexpected keyword argument 'kernel'"
- 指向对象生存期之外的已分配内存的指针是"invalid pointer[s]"还是"pointer[s] to an object"?
- 如何修复" State Error (active) E0513 a value of type "const wchar_t *" cannot be assigned to an entity o
- C++ libtins "An invalid handle was specified"
- "terminate called after throwing an instance of std::invalid_argument' what(): stoi ?"
- Hash for a std::pair, for use in an unordered_map
- Divide et impera sum of the elements of an array bug
- 为什么"an inherited constructor is not a candidate for initialization from an expression of the same or
- 如何理解pthread_cancel导致"terminate called without an active exception"?
- shared_ptr : "call of an object of a class type without appropriate operator() or conversion functio
- std::thread - "terminate called without an active exception" ,不想"加入"它