Thread local
suggest changeThis was a new storage specifier introduced in C11 along with multi-threading. This isn’t available in earlier C standards.
Denotes thread storage duration. A variable declared with _Thread_local
storage specifier denotes that the object is local to that thread and its lifetime is the entire execution of the thread in which it’s created. It can also appear along with static
or extern
.
#include <threads.h>
#include <stdio.h>
#define SIZE 5
int thread_func(void *id)
{
/* thread local variable i. */
static _Thread_local int i;
/* Prints the ID passed from main() and the address of the i.
* Running this program will print different addresses for i, showing
* that they are all distinct objects. */
printf("From thread:[%d], Address of i (thread local): %p\n", *(int*)id, (void*)&i);
return 0;
}
int main(void)
{
thrd_t id[SIZE];
int arr[SIZE] = {1, 2, 3, 4, 5};
/* create 5 threads. */
for(int i = 0; i < SIZE; i++) {
thrd_create(&id[i], thread_func, &arr[i]);
}
/* wait for threads to complete. */
for(int i = 0; i < SIZE; i++) {
thrd_join(id[i], NULL);
}
}
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents