Select your cookie preferences

We use essential cookies and similar tools that are necessary to provide our site and services. We use performance cookies to collect anonymous statistics, so we can understand how customers use our site and make improvements. Essential cookies cannot be deactivated, but you can choose “Customize” or “Decline” to decline performance cookies.

If you agree, AWS and approved third parties will also use cookies to provide useful site features, remember your preferences, and display relevant content, including relevant advertising. To accept or decline all non-essential cookies, choose “Accept” or “Decline.” To make more detailed choices, choose “Customize.”

Updated Mar 2025

Thread Local Storage Pointers

Introduction

Thread local storage (or TLS) allows the application writer to store values inside a task's control block, making the value specific to (local to) the task itself, and allowing each task to have its own unique value.

Thread local storage is most often used to store values that a single threaded program would otherwise store in a global variable. For example, many libraries include a global variable called errno. If a library function returns an error condition to the calling function, then the calling function can inspect the errno value to determine what the error was. In a single threaded application it is sufficient to declare errno as a global variable, but in a multi-threaded application each thread (task) must have its own unique errno value - otherwise one task might read an errno value that was intended for another task.

Thread Local Storage Pointers

FreeRTOS provides the application writer with a flexible thread local storage mechanism through the use of thread local storage pointers.

The configNUM_THREAD_LOCAL_STORAGE_POINTERS compile time configuration constant dimensions a per task array of void pointers (void*). The vTaskSetThreadLocalStoragePointer() API function is used to set a value within the array of void pointers, and pvTaskGetThreadLocalStoragePointer() API function is used to read a value from the array of void pointers.

Thread Local Integers

Values that have a size less than or equal to the size of a void pointer can be stored directly within the thread local storage pointer array. For example, if sizeof( void * ) is 4 then a 32-bit value can be stored in a void pointer variable, using a simple cast to avoid compiler warnings. However, if sizeof( void * ) is 2, then only a 16-bit value can be stored directly.

1uint32_t ulVariable;
2
3/* Write the 32-bit 0x12345678 value directly into index 1 of the thread
4 local storage array. Passing NULL as the task handle has the effect of writing
5 to the calling task's thread local storage array. */
6vTaskSetThreadLocalStoragePointer( NULL, /* Task handle. */
7 1, /* Index into the array. */
8 ( void * ) 0x12345678 );
9
10/* Store the value of the 32-bit variable ulVariable to index 0 of the calling
11 task's thread local storage array. */
12ulVariable = ERROR_CODE;
13
14vTaskSetThreadLocalStoragePointer( NULL, /* Task handle. */
15 0, /* Index into the array. */
16 ( void * ) ulVariable );
17
18/* Read the value stored in index 5 of the calling task's thread local storage
19 array into ulVariable. */
20ulVariable = ( uint32_t ) pvTaskGetThreadLocalStoragePointer( NULL, 5 );

Storing and retrieving 32-bit values directly from an index in the thread local storage array

Thread Local Structures

The previous example stored values directly into the thread local storage array. This next example demonstrates how to use a value in the array as a pointer to a structure that exists elsewhere in memory.

1typedef struct
2{
3 uint32_t ulValue1;
4 uint32_t ulValue2;
5} xExampleStruct;
6
7xExampleStruct *pxStruct;
8
9/* Create a structure for use by this task. */
10pxStruct = pvPortMalloc( sizeof( xExampleStruct ) );
11
12/* Set the structure members. */
13pxStruct->ulValue1 = 0;
14pxStruct->ulValue2 = 1;
15
16/* Store a pointer to the structure in index 0 of the calling task's thread
17 local storage array. */
18vTaskSetThreadLocalStoragePointer( NULL, /* Task handle. */
19 0, /* Index into the array. */
20 ( void * ) pxStruct );
21
22/* Locate the structure used by the calling task by reading its location from
23 index 0 of the calling task's thread local storage array. */
24pxStruct = ( xExampleStruct * ) pvTaskGetThreadLocalStoragePointer( NULL, 0 );

Storing a pointer to a structure in the calling task's thread local storage array