Home

Awesome

ThreadLocal

Portable, implementation configurable and c++11 thread_local compatible. The same code using macro THREADL_LOCA(T) supports different implementations controlled by macro USE_STD_THREAD_LOCAL.

The default implementation of macro THREAD_LOCAL(...) is c++11 thread_local keyword if supported. Otherwise, pthread and FLS implementation is used.

WHY

Limitation

Variables declared by THREAD_LOCAL(T)(actually only ThreadLocal<T>) must have static storage duration, i.e. usually static specifier is required.

Examples

    // declare and initialize. use THREAD_LOCAL macro to switch between c++11 thread_local keyword and ThreadLocal<T> implementation at build time using -DUSE_STD_THREAD_LOCAL=1/0
    static THREAD_LOCAL(int) a;
    static THREAD_LOCAL(int) b(1);
    static THREAD_LOCAL(int) c = 2;
    // assignment
    a = 3;
    // get address of stored thread data
    int* pa = &a;
    *pa = 4;
    // type convert
    printf("a=%d\n", (int&)a);

Tested Compilers

VS>=2013, gcc>=4.7, clang >=3.2, Apple clang, zpacc++1.0, icc>=16

Build