Coder Social home page Coder Social logo

threadlocal's Introduction

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

  • c++11 thread_local is not available for vs2013, macOS<10.10 and iOS, libc++ < 4.0(android ndk < r14), and non-trivial TLS destruction is not supported by MinGW clang.
  • std thread_local may have bugs, including mingw and android, see android-ndk/ndk issue 687

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

  • g++/clang++ c++11 thread_local: (clan)g++ -std=c++11 test.cpp
  • g++/clang++ pthread implementation: (clan)g++ -std=c++11 test.cpp -DUSE_STD_THREAD_LOCAL=0
  • apple clang (pthread implementation): clang++ -std=c++11 test.cpp
  • vs>=2015 c++11 thread_local: cl /EHsc test.cpp
  • vs>=2015 FLS implementation: cl /EHsc test.cpp -DUSE_STD_THREAD_LOCAL=0
  • vs2013 (FLS implementation): cl /EHsc test.cpp
  • mingw c++11 thread_local: g++ -std=c++11 test.cpp
  • mingw FLS implementation: g++ -std=c++11 test.cpp -D_WIN32_WINNT=0x0600 -DUSE_STD_THREAD_LOCAL=0
  • mingw pthread implementation: g++ -std=c++11 test.cpp -DUSE_STD_THREAD_LOCAL=0

threadlocal's People

Contributors

wang-bin avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.