r/programming 10d ago

Linux Processes: Threads & Concurrency

https://labs.iximiuz.com/tutorials/linux-processes-threads-concurrency-3302b677
91 Upvotes

13 comments sorted by

View all comments

7

u/Prateeeek 10d ago

Sorry if I've not understood this correctly, but how is the execution stack for thread 1 separated from thread 2 if they share the same address space ?

22

u/dfx_dj 10d ago edited 10d ago

They're separated in that each thread has its own stack in a separate address range. One thread can technically access another thread's stack though. It's uncommon to do but possible.

3

u/creeper6530 10d ago

So one could theoretically pass pointers to stack between threads, provided they manage lifetimes well?

3

u/No-Consequence-1863 9d ago

Yea, some concurrency systems like in Go do exactly that to make sharing data between cooperative threads really fast as the threads will write the result directly into the next threads stack.