Implementing TLS (Thread Local storage) for x86_64
kamkow1lair.pl·1d·
🔒BPF LSM
Preview
Report Post

31 January 2026

In this article I’d like to explore the implementation details of thread local storage on x86_64/amd64 for my operating system with compliance to System V ABI.

What is thread local storage?

Thread local storage is a type of storage in a multitasked application, where each task has it’s own copy of it, distinct from other tasks.

Example of TLS in C11

#include <threads.h>
#include <stdio.h>
#include <stdlib.h>

thread_local int counter = 0;

int thread_func(void *arg) {
int id = *(int*)arg;
counter++;  // Each thread increments its own copy
printf("Thread %d: counter = %d\n", id, counter);
return 0;
}

int main() {
thrd_t threads[4];
int ids[4] = {1, 2, 3, 4};

for (int i = 0; i < 4; i++) {
thrd_create(&threads[i], thread_func, &ids[i]);
}

for (int i = 0; i...

Similar Posts

Loading similar posts...

Keyboard Shortcuts

Navigation
Next / previous item
j/k
Open post
oorEnter
Preview post
v
Post Actions
Love post
a
Like post
l
Dislike post
d
Undo reaction
u
Recommendations
Add interest / feed
Enter
Not interested
x
Go to
Home
gh
Interests
gi
Feeds
gf
Likes
gl
History
gy
Changelog
gc
Settings
gs
Browse
gb
Search
/
General
Show this help
?
Submit feedback
!
Close modal / unfocus
Esc

Press ? anytime to show this help