In probing the conditions of this question, a problem arose, exemplified by the code below.
#include <iostream>
#include <thread>
#include <chrono>
#include <stdexcept>
#include <cxxabi.h>
using namespace std;
// mocking external library call stuck in an infinite loop
int buggy_function_simulation()
{
// cout << "In buggy function" << endl; // (1)
int counter = 0;
while (true)
{
if ( ++counter == 1000000 ) { counter = 0; }
}
}
int main(int argc, char **argv) {
cout << "Hello, world!" << endl;
auto lambda = []() {
pthread_setcanceltype( PTHREAD_CANCEL_ASYNCHRONOUS, nullptr );
// cout << "ID: "<<pthread_self() <<endl; // (2)
try
{
cout << "ID: "<<pthread_self() <<endl; // (3)
buggy_function_simulation();
}
catch ( abi::__forced_unwind& )
{
cout << "thread cancelled!" << endl; // (4)
throw;
}
};
std::thread th(lambda);
pthread_t id = th.native_handle();
cout << id << endl;
this_thread::sleep_for(chrono::seconds(1));
cout << "cancelling ID: "<< id << endl;
pthread_cancel(id);
th.join();
cout << "cancelled: "<< id << endl;
return 0;
}
Compiling and running results in an abort:
$ g++ -g -Og -std=c++11 -pthread -o test test.cpp -lpthread
$ ./test
Hello, world!
139841296869120
ID: 139841296869120
cancelling ID: 139841296869120
terminate called without an active exception
Aborted (core dumped)
$
Note that the diagnostic output (4) does not appear.
If I comment out (3) and uncomment (2), the result is:
$ ./test
Hello, world!
139933357348608
ID: 139933357348608
cancelling ID: 139933357348608
cancelled: 139933357348608
$
Again, the output at (4) does not appear (why?), but the abort has been obviated.
If, alternately, I retain (3), leave (2) commented out, and uncomment (1), the result is finally as expected:
$ ./test
Hello, world!
139998901511936
ID: 139998901511936
In buggy function
cancelling ID: 139998901511936
thread cancelled!
cancelled: 139998901511936
$
So, the questions are:
- what is the reason for the "terminate called without an active exception" abort in the first case?
- why is the catch block not activated in the second case?
- why did uncommenting (1) in the third case make such a difference?
For completeness, here is the stack trace from gdb for the first case:
Program terminated with signal SIGABRT, Aborted.
#0 __GI_raise (sig=sig@entry=6) at ../sysdeps/unix/sysv/linux/raise.c:51
51 ../sysdeps/unix/sysv/linux/raise.c: No such file or directory.
[Current thread is 1 (Thread 0x7f5d9b49a700 (LWP 12130))]
(gdb) where
#0 __GI_raise (sig=sig@entry=6) at ../sysdeps/unix/sysv/linux/raise.c:51
#1 0x00007f5d9b879801 in __GI_abort () at abort.c:79
#2 0x00007f5d9bece957 in ?? () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#3 0x00007f5d9bed4ab6 in ?? () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#4 0x00007f5d9bed4af1 in std::terminate() () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#5 0x00007f5d9bed44ba in __gxx_personality_v0 () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#6 0x00007f5d9bc3a708 in ?? () from /lib/x86_64-linux-gnu/libgcc_s.so.1
#7 0x00007f5d9bc3acfc in _Unwind_ForcedUnwind () from /lib/x86_64-linux-gnu/libgcc_s.so.1
#8 0x00007f5d9c1dbf10 in __GI___pthread_unwind (buf=<optimized out>) at unwind.c:121
#9 0x00007f5d9c1d0d42 in __do_cancel () at ./pthreadP.h:297
#10 sigcancel_handler (sig=<optimized out>, si=0x7f5d9b499bb0, ctx=<optimized out>) at nptl-init.c:215
#11 <signal handler called>
#12 buggy_function_simulation () at test.cpp:15
#13 0x0000558865838227 in <lambda()>::operator() (__closure=<optimized out>) at test.cpp:28
#14 std::__invoke_impl<void, main(int, char**)::<lambda()> > (__f=...) at /usr/include/c++/7/bits/invoke.h:60
#15 std::__invoke<main(int, char**)::<lambda()> > (__fn=...) at /usr/include/c++/7/bits/invoke.h:95
#16 std::thread::_Invoker<std::tuple<main(int, char**)::<lambda()> > >::_M_invoke<0> (this=<optimized out>)
at /usr/include/c++/7/thread:234
#17 std::thread::_Invoker<std::tuple<main(int, char**)::<lambda()> > >::operator() (this=<optimized out>)
at /usr/include/c++/7/thread:243
#18 std::thread::_State_impl<std::thread::_Invoker<std::tuple<main(int, char**)::<lambda()> > > >::_M_run(void) (
this=<optimized out>) at /usr/include/c++/7/thread:186
#19 0x00007f5d9beff66f in ?? () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#20 0x00007f5d9c1d26db in start_thread (arg=0x7f5d9b49a700) at pthread_create.c:463
#21 0x00007f5d9b95a88f in clone () at ../sysdeps/unix/sysv/linux/x86_64/clone.S:95
Aucun commentaire:
Enregistrer un commentaire