C11およびC++11の言語仕様に追加されたThread Local Storage(TLS)変数についてメモ。
あるスレッドに属するTLS変数に対して他スレッドから間接的にアクセスした場合、その挙動はC11では処理系定義(implementation-defined)とされる。一方、C++11では同TLS変数の生存期間内であれば他スレッドから間接アクセスが可能。(N2985)
C++11
JTC1/SC22/WG21 N3337 3.7.2/p1より引用。
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.
C11
JTC1/SC22/WG14 N1570 6.2.4/p4より引用(下線部は強調)。
An object whose identifier is declared with the storage-class specifier
_Thread_local
has thread storage duration. Its lifetime is the entire execution of the thread for which it is created, and its stored value is initialized when the thread is started. There is a distinct object per thread, and use of the declared name in an expression refers to the object associated with the thread evaluating the expression. The result of attempting to indirectly access an object with thread storage duration from a thread other than the one with which the object is associated is implementation-defined.
関連URL
- JTC1/SC22/WG21 N2985 C and C++ Thread Compatibility (WG14 N1423)
- JTC1/SC22/WG21 N2659 Thread-Local Storage
- JTC1/SC22/WG14 N1329 Thread-local storage