Pthread exit
현재 실행중인 쓰레드를 종료한다.
Synopsis
Troubleshooting
FATAL: exception not rethrown
아래와 같은 에러가 발생될 수 있다.
FATAL: exception not rethrown
A 2019-12-20T12:39:03.230677552 @140313816094464 Abort signal. (SIGABRT):
#00 0x00007F9D626EDF20 +257824 [/lib/x86_64-linux-gnu/libc.so.6]
#01 0x00007F9D626EDE97 gsignal+199 [/lib/x86_64-linux-gnu/libc.so.6]
#02 0x00007F9D626EF801 abort+321 [/lib/x86_64-linux-gnu/libc.so.6]
#03 0x00007F9D627385E5 +562661 [/lib/x86_64-linux-gnu/libc.so.6]
#04 0x00007F9D6273892A +563498 [/lib/x86_64-linux-gnu/libc.so.6]
#05 0x00007F9D66078ED0 __pthread_unwind+0 [/lib/x86_64-linux-gnu/libpthread.so.0]
#06 0x00007F9D63B732FD libtbag::thread::__global_uv_thread_cb__(void*)+223 [/home/user/Project/tbag/cmake-build-debug/libtbag.so.0]
#07 0x00007F9D6606F6DB +30427 [/lib/x86_64-linux-gnu/libpthread.so.0]
#08 0x00007F9D627D088F clone+63 [/lib/x86_64-linux-gnu/libc.so.6]
pthread는 Stack unwinding과 관련된 자원 해제가 끔찍하다. 예를 들면, 스레드 함수에서 catch
문을 사용할 때 문제가 발생된다. 원문은 아래와 같다.
Sorry to be the bearer of bad news but it is very hard to get pthread_cancel to work properly in C++ in Linux.
The reason is that the pthread_cancel method implementation in Linux is absolutely horrid. In particular it causes an exception (abi::__forced_unwind as of the time I'm writing this) to be thrown. This probably is fine in a C program, but in C++ it makes it very difficult to handle the thread cancellation properly. In particular if you have any code that eats exceptions (such as catch (...)) this internal exception will handled and not passed up the stack the way it needs to be. In this case you will see the error message that you are seeing.
That said, I don't see what in your code would be "eating" this exception, but I suspect it is something of that nature that is causing the problem.
In your particular example this is easy to work around by using a flag or something similar to note when you want to exit and not call pthread_cancel. Unfortunately that makes sleep more complex (you need to call it multiple times with small intervals in order to check the flag) and calls to accept nearly impossible (you have to make a connection to the port from another thread or process to force accept to exit so you can check the variable). There is a long discussion on the problems with thread cancellation in general ([https://lwn.net/Articles/683118/ This is why we can't have safe cancellation points]) but given that macOS (and presumably other non-Linux "unixes") seem to handle it well, I really think that the Linux implementation could be done better. But until that is done, pthread_cancel will remain very difficult to use.
상세 내용은 Thread: pthread_exit, c++ and FATAL: exception not rethrown 게시물을 참조했다. 중요한 내용은 아래와 같다.
- pthread_exit doesn't cope with catch (...)
- we need catch (abi::__forced_unwind&) throw; to get it work
- pthread_exit does stack unwinding and deletes the objects on the stack!
- bug: there isn't even one word about all this in man pthread_exit
별도로, abi::__forced_unwind
를 사용한 코드는 아래와 같다.
#if defined(__linux__) && !defined(__ANDROID__)
#include <cxxabi.h>
#ifdef _CXXABI_FORCED_H
#define SPDLOG_CATCH_ALL catch (abi::__forced_unwind&) { _err_handler("Unknown exception"); throw; } catch (...)
#else // _CXXABI_FORCED_H
#define SPDLOG_CATCH_ALL catch (...)
#endif // _CXXABI_FORCED_H
#else // __linux__
#define SPDLOG_CATCH_ALL catch (...)
#endif // __linux