Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple threads reading static variable at the same time

My question may be newbie or duplicate, but i wonder what is happening when several threads try to read a static variable at the same time. I'm not interesting in synchronization now, i just want to know are they reading it instantly or by turn?

UPDATE: my question is more in domain of physics or smth like that(= if it is the same moment of time when threads read the variable.

like image 824
donRumatta Avatar asked Dec 07 '11 12:12

donRumatta


2 Answers

If a value of variable does not change (any thread does not write a value) so read by multiple threads would be a safe operation and does not require an additional synchronization like locking. Otherwise you have to consider locking for write access operations.

UPDATE: Regarding question update

Physically in scope of a single core CPU only one instruction (simplified, ignore CPU pipelines) could be executed so no chance to access the same memory location in a same quant of a time.

like image 117
sll Avatar answered Sep 28 '22 02:09

sll


They can't be accessing it truly simultaneously - at some point the CPU will be sequencing the reads.

like image 33
Cylindric Avatar answered Sep 28 '22 00:09

Cylindric