[cfe-dev] status of 'thread_local'

Tim Northover t.p.northover at gmail.com
Tue Oct 16 14:26:46 PDT 2012


> What is the lifetime on a __thread variable?  It gets destructed at the time
> the thread exits?

Pretty much. From C++11 (3.7.2):

"All variables declared with the thread_local keyword have thread
storage duration. The storage for these
entities shall last for the duration of the thread in which they are
created. There is a distinct object or
reference per thread, and use of the declared name refers to the
entity associated with the current thread."

"A variable with thread storage duration shall be initialized before
its first odr-use (3.2) and, if constructed,
shall be destroyed on thread exit."

I wouldn't swear to LLVM implementing these semantics yet though. The
initial TLS specification forbade non-trivial constructors and had no
mention of destructors. In fact, from a quick test it looks like LLVM
destroys the object at program exit, and only in one thread (unless
__cxa_atexit and to a lesser extent .init_array are more magical than
I knew).

Tim.



More information about the cfe-dev mailing list