8

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.

Jim Aho
  • 9,932
  • 15
  • 56
  • 87
donRumatta
  • 836
  • 2
  • 9
  • 22
  • 5
    If you fear that it is a duplicate, you should have searched first. – PVitt Dec 07 '11 at 12:50
  • I suppose this is done turn by turn, though the time between reads is very very small – Thomas Dec 07 '11 at 12:51
  • what is the type of a variable? – abatishchev Dec 07 '11 at 12:51
  • @abatishchev, more often it is reference type – donRumatta Dec 07 '11 at 13:03
  • this is a good question. assuming you have multiple cores so two instructions can execute simultaneously, with the right type of memory i believe you can have parallel access at the same location. GPU memory is optimized for this sort of thing. i.e. i believe nvidia GPUs have this capability -- would suggest reading the CUDA manual. and also http://www.akkadia.org/drepper/cpumemory.pdf – Jesse Cohen Dec 08 '11 at 04:07

3 Answers3

15

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.

sll
  • 61,540
  • 22
  • 104
  • 156
  • It wasn't my down-vote, but I imagine it was because whilst the statement is correct, the question was nothing to do with thread-safe writes, locking, or synchronising. – Cylindric Dec 07 '11 at 13:01
  • @Cylindric, yes, 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. – donRumatta Dec 07 '11 at 13:09
  • In that case AnthonyLambert's response below is probably the most relevant. You can't read the same bit of memory twice at the same point in time. The nuts and guts of it are quite complex though, as a modern multi-core CPU maintains all sorts of cache about these sorts of things. – Cylindric Dec 07 '11 at 13:18
  • Hi @sll what would happen if LITERALLY a variable is updated by one thread and another thread reads it EXACTLY AT THE SAME TIME (Considering there is no synchronization)? Thanks in advance –  Nov 01 '22 at 19:01
3

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

Cylindric
  • 5,858
  • 5
  • 46
  • 68
  • 4
    what about multi-CPU computer ? :/ – Guillaume Slashy Dec 07 '11 at 12:54
  • 1
    In that case the answer is probably "it depends" - but I doubt they would be reading the *same* variable, but two copies - one on each CPU? Either way, who cares? They're static. And the OP specifies they are not changing, so not static objects being manipulated. Does it matter? It might as well be burned into the CPU die. – Cylindric Dec 07 '11 at 12:57
  • 3
    @Guillaume Slashy: Access to the same memory module is serialized no matter how many cores you have. Yes the read instructions are issued in parallel, but you cannot access a single memory module in parallel anyway. – Tudor Dec 07 '11 at 12:57
  • 2
    If the variable is not a single memory location, for example a string, then it is possible for one thread to be modifying it while another is reading it. The result could be reading a string made of some unknown combination of the two operations which would cause untold random bugs. – AnthonyLambert Dec 07 '11 at 13:02
  • 1
    @tudor: in the case of a type like a string, you can effectly access a variable stored in a memory module concurently across threads. – AnthonyLambert Dec 07 '11 at 13:09
  • 1
    @Tudor: When you say that access to a single memory location is serialized no matter what, does that account for what Cylindric was hinting at, about multiple Cores cash's containing a copy of the value? – DorD Dec 08 '11 at 10:38
  • 2
    @DorD: They are two different things. Yes, the cores will bring a separate copy in each of their private caches, but when the read requests for the same memory location reach the memory module, they cannot occur at the same time. – Tudor Dec 08 '11 at 11:03
0

If it is a static type that is read in one go a processor core (on all platforms) then it is an atomic operation. If it is a larger type that takes more than one operation to read or write then it is not atomic and you could read dodgy values that are a product of another thread changing it partially while you are reading/writing it.

AnthonyLambert
  • 8,768
  • 4
  • 37
  • 72
  • 2
    The atomicity is irrelevant. This is asking about multiple threads *reading* the value. Even if the read is a single instruction, it will still happen in sequence, unless the threads are running on different cores. – Cylindric Dec 07 '11 at 12:59
  • 1
    eh? these days it is very common to be running across multiple cores processors... – AnthonyLambert Dec 07 '11 at 13:13
  • 1
    So you initialise your static variable in a constructor are you sure no other thread is accessing that static before or during? – AnthonyLambert Dec 07 '11 at 13:14
  • Depends if you mean the static constructor, or some other constructor. Static constructors in C# are guaranteed to be called on first access of the static type, and guaranteed to be thread safe. So even if two places do access at the same time, one wins and the other waits. – James Aug 10 '23 at 13:54
  • A variable could be larger than the processor can handle in a single read like a long long. In that case another process could change half way through a read and give bad results. – AnthonyLambert Aug 22 '23 at 14:14