c++ - How to only disable destruction of objects in thread-local-storage? -
#include <cstdlib> #include <thread> #include <chrono> using namespace std; using namespace std::literals; struct { a() { cout << "a()" << endl; } ~a() { cout << "~a()" << endl; } }; g_a; int main() { thread([]() { thread_local a; this_thread::sleep_for(24h); }).detach(); exit(0); // not ok quick_exit(0); // still not ok }
- global objects , objects in
thread-local-storage
destructed after callingexit(0)
; - global objects , objects in
thread-local-storage
not destructed after callingquick_exit(0)
;
is possible:
global objects destructed after calling some_exit_func(0)
; while objects in thread-local-storage
not destructed.
problem background:
i have big legacy project, has used
thread-local-storage
store many c++ objects; before c++11, these objects not destructed automatically, former writer explicitly calls destructors.now, want recompile c++11 compiler; faced such problem:
- if use
exit(0)
, objects inthread-local-storage
double-destructed;- if use
quick_exit(0)
, global variables not destructed.
Comments
Post a Comment