user16011868
user16011868

Reputation:

Threads Share local variable?

I learnt that threads share same memory but don't share registers.

On the other hand, I also read that threads don't share local variables, how is that possible?

Isn't this a contradiction?

Upvotes: 1

Views: 88

Answers (1)

Matteo T.
Matteo T.

Reputation: 39

I also read that threads don't share local variables, how is that possible?

Threads of the same process share the same address space and they follow the same scope rules of blocks and functions, so if you declare a global variable or you pass a variable to the thread as a pointer argument, this variable is shared between threads and you need various precautions in order to modify it (synchronization).

I learnt that threads share same memory but don't share registers.

Threads don't shares registers because each thread runs on a different core (this is a simplification because a core can have multiple threads in modern architectures) and each core has their own registers, so you need synchronization methods just for handle this problem.

Upvotes: 1

Related Questions