pthread_cancel and C++11

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



My organization recently upgraded to C++11 and we're running into a
problem. We use the POSIX threading library, and in some rare cases
(pthread_join doesn't exit within a specified timeout, for example),
we'll kill a thread via pthread_cancel. In C++, thread cancellation is
implemented via the abi::__forced_unwind exception[1], and in C++03,
this worked well for us.

In C++11, all destructors are noexcept(true) by default. So if you try
to cancel a thread that's in a destructor, the abi::__forced_unwind
exception will escape the destructor, std::terminate() will be
invoked, and your program dies. I have written a simple program that
can reproduce this.[2]

What's the best way to solve this problem? I could avoid using
pthread_cancel, but I was hoping to be able to upgrade to C++11
without making major changes like that. I could flag all my
destructors as noexcept(false), but that's tedious and doesn't cover
third party code like the STL or Boost. I briefly looked into
std::thread, but it looks like std::thread doesn't have a cancellation
mechanism. I have reproduced this with a couple of different libstdc++
versions (4.8.4-2ubuntu1~14.04 being the newest package I have tested
with). Would upgrading to a newer version fix my problem? Any other
ideas? Do I just need to accept the fact that C++11 broke POSIX thread
cancellation and there's always going to be a chance that
pthread_cancel will cause my application to terminate?

Thanks,
John

[1] http://udrepper.livejournal.com/21541.html
[2] https://gist.github.com/jsaxton/dc601e29fbe8c7c8f1b2



[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux