This is the documentation for the latest (main) development branch of Zephyr. If you are looking for the documentation of previous releases, use the drop-down menu on the left and select the desired version.

Thread Local Storage (TLS)

Thread Local Storage (TLS) allows variables to be allocated on a per-thread basis. These variables are stored in the thread stack which means every thread has its own copy of these variables.

Zephyr currently requires toolchain support for TLS.

Configuration

To enable thread local storage in Zephyr, CONFIG_THREAD_LOCAL_STORAGE needs to be enabled. Note that this option may not be available if the architecture or the SoC does not have the hidden option CONFIG_ARCH_HAS_THREAD_LOCAL_STORAGE enabled, which means the architecture or the SoC does not have the necessary code to support thread local storage and/or the toolchain does not support TLS.

CONFIG_ERRNO_IN_TLS can be enabled together with CONFIG_ERRNO to let the variable errno be a thread local variable. This allows user threads to access the value of errno without making a system call.

Declaring and Using Thread Local Variables

The keyword __thread can be used to declare thread local variables.

For example, to declare a thread local variable in header files:

extern __thread int i;

And to declare the actual variable in source files:

__thread int i;

Keyword static can also be used to limit the variable within a source file:

static __thread int j;

Using the thread local variable is the same as using other variable, for example:

void testing(void) {
    i = 10;
}