0

The scope of my question is STRICTLY limited to the simultaneous read and write of an address in memory and what happens in hardware.

I am not interested in knowing about race conditions and what they might carry AFTER the program continues executing by using a value where this race occurred. If the logic of a pc is broken, yes, they might carry also a crash, but here my question is strictly about hardware and the effect of WRITE+READ from simultaneous threads.

QUESTION: Suppose I write a variable from one thread and I read from another thread the same variable. What will happen?

  1. just corrupt the value of the variable , so that all following thread will read a wrong value due to the interleave of this read/write?

or

  1. in certain case, can also cause the app to go down because this might be in some case some kind of violation in hardware.

Essentially I want to know more about the process of physically reading and writing at the same time of a variable and what the PC behaves.

I am using LINUX and C++ and I am interested in 2 cases

  • 2 threads on the same physical core

  • 2 threads on 2 separate cores

Abruzzo Forte e Gentile
  • 14,423
  • 28
  • 99
  • 173

1 Answers1

1

It all depends on what kind of hardware and what size of variable. Access to integers is often atomic, but access to doubles is usually not. Even for simple small values different hardware may have very different behaviour. You need to get a copy of the hardware manual. Here's a post that discusses some of this area: Does one assembler instruction always execute atomically?

Reads will not change the behaviour of writes unless the hardware is poorly designed, but a write can cause the value being read to be incorrect if the read sees part of the write but not the rest.

Community
  • 1
  • 1
Peter Wooster
  • 6,009
  • 2
  • 27
  • 39
  • 1
    Even for multi-word operations, the worst scenario that can happen is reading a corrupt value, right? I think only by register indirect adressing this incorrect value may lead to a memory violation. – A. Rodas Mar 20 '13 at 00:52
  • @Thank Peter. I got it and I need to confirm last part of your response: suppose that happened once that the value being read is incorrect because of a concurrent write. What happens on the NEXT read of that variable (with no concurrent write)? The first read was corrupted by the concurrent write, but the second time will the read return the right variable? I guess so as I understood that the write behaviour is not altered by the write ( and this would not be the case if there were 2 concurrent writings ). – Abruzzo Forte e Gentile Mar 20 '13 at 08:52
  • @AbruzzoForteeGentile subsequent reads will be good, the write would not be affected so the stored data I'll be correct. With concurrent writes all bets are off. – Peter Wooster Mar 21 '13 at 00:55
  • @Peter. Hi thank you very much for this response. All very clear! – Abruzzo Forte e Gentile Mar 21 '13 at 01:15