Is there any way to fully emulate thread_local using GCC's __thread?

后端 未结 1 690
失恋的感觉
失恋的感觉 2021-01-14 03:12

The C++11 standard contains a new addition - the thread_local specifier - which makes static variables thread-local. The standard thread_local supports non-trivial types - t

相关标签:
1条回答
  • 2021-01-14 03:37

    no.

    gcc does currently not have the ability to run ctor/dtor for __thread stuff on thread creation/destruction, so unless you dont need ctor/dtor to run (in which case __thread is exactly what you need, and nothing to emulate on top of it is needed), there is nothing yet that works like thread_local.

    If however you can live with lazy initialization (like__thread T* ptr; if(!ptr){...}) you can hack something together with pthread_key_create where you can register a destruction function that will be run at thread destruction, and then you can register all your pointers there.

    Or you can use boost::thread_specific_ptr which more or less does this (possibly without using the __thread TLS as underlying implementation detail)

    0 讨论(0)
提交回复
热议问题