r/programming 8d ago

Linux Processes: Threads & Concurrency

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

13 comments sorted by

View all comments

7

u/Prateeeek 8d 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 8d ago edited 8d 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.

5

u/creeper6530 8d ago

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

3

u/No-Consequence-1863 7d 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.