Loading...
Searching...
No Matches
pico_thread_local

C/C++ __thread/thread_local support.

C/C++ __thread/thread_local support.

This library provides transparent runtime support for thread locals for the different types of thread local implementations used by the SDK supported compilers and C libraries.

This means that by default, each core will get its own value for the thread local variable, and the core 1 value will be re-initialized if the core is restarted.

Additionally, this library provides the "picolibc" style methods _tls_size(), _init_tls() and _set_tls() even when not using picolibc, which makes it easy to enable thread locals per task on at least FreeRTOS (just set #define configUSE_PICOLIBC_TLS 1 in your FreeRTOSConfig.h).

The pico_thread_local library comes in three main flavors:

  1. per_thread (default) - Full support for thread local variables. Each thread/core gets its own copy of the variable initialized when it starts executing. The library tries to minimize the overhead if there are no thread locals used, however particularly on RISC-V there is always a small overhead.
  2. global - Thread local variables are allowed in code, but each thread/core shares the same value (i.e. they aren't really thread local). There is very minimial overhead for this option.
  3. none - No support for thread locals is provided. Code using them may not compile or link, and if it does the values won't be shared and may not even be initialized correctly. This mode is however guaranteed to have basically no overhead when thread locals are known not to be used.

The support provided by the pico_thread_local library may be set from CMake via pico_set_thread_local_implementation(<TARGET> per_thread|global|none) and the default for all targets may be set via CMake variable (e.g. set(PICO_DEFAULT_THREAD_LOCAL_IMPL global)).