r/programming • • Aug 05 '26

Linux Processes: Threads & Concurrency

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

12 comments sorted by

View all comments

Show parent comment

5

u/Prateeeek Aug 05 '26

So, in the same space, but distinct address blocks?

Also the latter part of your sentence maybe explains why in java a variable local to a function needs to be immutable if it's being referenced in a lambda (closure of sorts, which may/may not be executed in a different thread)

18

u/Arnavion2 Aug 05 '26 edited Aug 05 '26

The start of a stack is just a pointer. It can be anywhere in memory.

The main thread's stack pointer is set by the binary loader. For ELF binaries the loader will reserve space for the stack, set the stack pointer register to the start of that space, and then jump to the binary entrypoint. For spawned threads, the thread spawning API will reserve some space for the new thread's stack, set the stack pointer register to the start of that space, and then jump to the thread entrypoint.

(Note: "Start of the stack" is often the highest address of the stack, not the lowest, because on many architectures the stack grows "downward", ie starts from the high address end and grows towards the low address end.)