简体   繁体   English

带有pthread析构函数的c ++ thread_local析构函数

[英]c++ thread_local destructors with pthread destructors

I want to do some work after all C++ thread_local destructors called. 我想在所有C ++ thread_local析构函数调用之后做一些工作。 This is platform specific - Android, so I have access to pthreads . 这是特定于平台的-Android,因此我可以访问pthreads

The question is, when pthread_key_create d destructors should be called, before or after C++ thread_local destructors? 问题是,何时应在C ++ thread_local析构函数之前或之后调用pthread_key_create d析构函数? Or they can be interleaved? 还是可以交错?

I tested On Linux Mint and pthread destructors called after C++ 's. 我在Linux Mint和以C ++命名的pthread析构函数上进行了测试。

bionic/pthread_exit.cpp currently has the same order: bionic / pthread_exit.cpp当前具有相同的顺序:

void pthread_exit(void* return_value) {
  // Call dtors for thread_local objects first.
  __cxa_thread_finalize();
  // Call the TLS destructors.
  pthread_key_clean_all();

However, this is not documented behavior and you should not build something relying on it. 但是,这不是已记录的行为,因此您不应构建依赖它的行为。

libstdc++ from GCC uses pthread_key_create in case the platform does not provide __cxa_thread_atexit_impl . 如果平台不提供__cxa_thread_atexit_impl ,则GCC的libstdc++使用pthread_key_create In this case, C++ destructors run somewhere in the middle of the POSIX destructors. 在这种情况下,C ++析构函数在POSIX析构函数的中间位置运行。

To my knowledge, there is no standard which requires any particular behavior here because C++ does not know about POSIX and POSIX does not know about C++, so neither standard says what happens here. 据我所知,这里没有标准需要任何特殊的行为,因为C ++不了解POSIX,POSIX不了解C ++,因此没有一个标准说明这里发生了什么。 There are also some corner cases involving the resurrection of thread-local data during thread destruction which will vary among implementations. 还有一些极端情况涉及线程销毁期间线程本地数据的复活,具体情况因实现而异。 (A typical example is a per-thread logger object which is used to log from destructors of thread-local variables.) (一个典型的示例是每个线程记录器对象,该对象用于从线程局部变量的析构函数进行记录。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM