pthread_key_create析构函数未得到调用 [英] pthread_key_create destructor not getting called
问题描述
根据 pthread_key_create 手册页,我们可以关联一个析构函数在线程关闭时被调用.我的问题是我已注册的析构函数没有被调用.我的代码要点如下.
As per pthread_key_create man page we can associate a destructor to be called at thread shut down. My problem is that the destructor function I have registered is not being called. Gist of my code is as follows.
static pthread_key_t key;
static pthread_once_t tls_init_flag = PTHREAD_ONCE_INIT;
void destructor(void *t) {
// thread local data structure clean up code here, which is not getting called
}
void create_key() {
pthread_key_create(&key, destructor);
}
// This will be called from every thread
void set_thread_specific() {
ts = new ts_stack; // Thread local data structure
pthread_once(&tls_init_flag, create_key);
pthread_setspecific(key, ts);
}
有什么想法可以阻止此析构函数被调用?我现在也在使用atexit()在主线程中进行一些清理.是否有可能干扰析构函数的调用?我也尝试将其删除.仍然没有工作.我也不清楚是否应该使用atexit将主线程作为单独的案例处理. (顺便说一句,必须使用atexit,因为我需要在应用程序出口处进行一些特定于应用程序的清理)
Any idea what might prevent this destructor being called? I am also using atexit() at moment to do some cleanup in the main thread. Is there any chance that is interfering with destructor function being called? I tried removing that as well. Still didn't work though. Also I am not clear if I should handle the main thread as a separate case with atexit. (It's a must to use atexit by the way, since I need to do some application specific cleanup at application exit)
推荐答案
这是设计使然.
主线程退出(通过返回或调用exit()
),并且不使用pthread_exit()
. POSIX文档pthread_exit
调用了特定于线程的析构函数.
The main thread exits (by returning or calling exit()
), and that doesn't use pthread_exit()
. POSIX documents pthread_exit
calling the thread-specific destructors.
您可以在main
的末尾添加pthread_exit()
.另外,您可以使用atexit
进行销毁.在那种情况下,将特定于线程的值设置为NULL
是干净的,因此,在调用pthread_exit
的情况下,对该键的销毁不会发生两次.
You could add pthread_exit()
at the end of main
. Alternatively, you can use atexit
to do your destruction. In that case, it would be clean to set the thread-specific value to NULL
so in case the pthread_exit
was invoked, the destruction wouldn't happen twice for that key.
更新实际上,我已经解决了我的紧迫问题,只需将其添加到我的全局单元测试设置功能中即可.
UPDATE Actually, I've solved my immediate worries by simply adding this to my global unit test setup function:
::atexit([] { ::pthread_exit(0); });
因此,在我的全局夹具类MyConfig
中:
So, in context of my global fixture class MyConfig
:
struct MyConfig {
MyConfig() {
GOOGLE_PROTOBUF_VERIFY_VERSION;
::atexit([] { ::pthread_exit(0); });
}
~MyConfig() { google::protobuf::ShutdownProtobufLibrary(); }
};
使用了一些参考文献:
- http://www.resolvinghere.com/sof/6357154.shtml
- https://sourceware.org/ml/pthreads-win32/2008/msg00007.html
- http://pubs.opengroup.org/onlinepubs/009695399/functions/pthread_key_create.html
- http://pubs.opengroup.org/onlinepubs/009695399/functions/pthread_exit.html
- http://www.resolvinghere.com/sof/6357154.shtml
- https://sourceware.org/ml/pthreads-win32/2008/msg00007.html
- http://pubs.opengroup.org/onlinepubs/009695399/functions/pthread_key_create.html
- http://pubs.opengroup.org/onlinepubs/009695399/functions/pthread_exit.html
PS.当然,c ++ 11 引入了<thread>
,因此您拥有更好,更便携的灵长类动物与之共事.
PS. Of course c++11 introduced <thread>
so you have better and more portable primitves to work with.
这篇关于pthread_key_create析构函数未得到调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!