chsvlib
chsv helper source code

◆ chsv_thread_local

#define chsv_thread_local (   type,
  name 
)    thread_local type name

A macro simplifying work with thread-specific storages. It is introduced to implement syntax similar to the one defined by the C++11 thread_local keyword.

Parameters
typedefines a type of a thread-local object. The type must satisfy the requirements of CopyConstructible.
nameis a name of a thread-specific variable.

The macro and implementing routines are introduced in order to provide portable interface to the thread-specific storages similar to the one provided by the thread_local C++11 keyword. The the underlying TSS mechanism is also portable and provided with an interface based on the C11 standard. However, there are two constraints on the use of the macro. First, the type of the thread-local object must be CopyAssignable. Second, according to the C++11 standard (sections 7.1.1.4 and 9.2.6) if the thread_local keyword is used in a block scope, the static storage class specifier is implied or set explicitly, and, if the thread-specific object is defined as a class member, the one is required to be explicitly static. To provide compatibility of the client code for various compilers it was decided to exclude the static keyword from the macro definition. It means that explicit static object specification should be mandatory in a block or a class scope. The lack of the specification will result to rare runtime-errors which are hard to debug, since it is impossible to clarify a context of the macro usage.

For compilers that support the thread_local keyword the macro resolves to the one.

Warning
Always use the static storage duration with the macro used in a class or a block scope.
To provide compatibility of the client code with the standard thread_local mechanism use chsv_thread_local_get to access elements of compound thread-specific objects.
Examples
chsvthrd\chsv_thread_local_1.cpp, chsvthrd\chsv_thread_local_2.cpp, and chsvthrd\chsv_thread_local_get.cpp.