1

If we perform a atomic operation on a multi core cpu, does the atomic instruction freeze operations on all other cores?

Example if we do a increment on a atomic variable: ++atomicInteger; Does this freeze all other operations on other cores? I am focused on x86 processors.

I know that reading or writing to memory aligned native type is atomic and does not impact any other cores execution.

4d.coder
  • 127
  • 2
  • 12

2 Answers2

2

x86 allows writing unaligned data that spans across two cache lines (i.e. across two 64 byte chunks), but the result is not guaranteed to be atomic. This means you may read 8 Byte from addr 0x1003c for e.g., requiring the CPU to fetch 2 lines (0x10000 and 0x10040), taking the relevant 4-byte chunks and stitching them together. However, these two lines may be stored in different locations - one could be cached, the other could be in the main memory. In extreme cases (page splits), one could in theory even be swapped out. As a result, you might get 2 data chunks from different times (a better term is observation points), where a store from some other process could have changed one in the middle.

On the other hand, once you add the lock prefix (or add an std::atomic definition, which should include that for you), x86 does guarantee that the result comes from a single observation point, and is consistent with observations from all other threads. To achieve this, it's quite possible that the CPU will enforce a complete block of all cores (for e.g. bus lock) until both lines are secured in the requesting core. If you don't you're risking a livelock where you constantly get one line, and lose it to another core by the time you got the second.

p.s. - user3286380 raised a good point, ++atomicInteger is not atomic, even if you declared it as such. std::atomic guarantees an atomic read and an atomic write (each on its own observation point), but it doesn't guarantee atomic read-modify-write unless you explicitly state that.

Leeor
  • 19,260
  • 5
  • 56
  • 87
0

An atomic operation is an operation that cannot be done by multiple processors at the same time. If you want to do an addition atomically only one thread can be doing that operation.

If we perform a atomic operation on a multi core cpu, does the atomic instruction freeze operations on all other cores?

No. Not necessarily, if you happen to have multiple threads trying to do the same atomic operation, than they will be halted except the first one to reach that atomic statement.

I know that reading or writing to memory aligned native type is atomic and does not impact any other cores execution.

Where did you read this? It does not sound correct to me. The result of this operation might depend on the architecture. But if you have multiple threads on x86 for example, and those threads try to write to the same location, the operation is not atomic by default. So the final value of the address that is being edited by threads can be anything.

Here is a similar discussion you might be interested in : pthreads: If I increment a global from two different threads, can there be sync issues?

Community
  • 1
  • 1
  • 1
    Reading and writing are separately atomic on most platforms. Doing both is not. – user3286380 Mar 09 '14 at 03:49
  • @user3286380 You are right, isn't it trivial though? Are there any cases where just writing to a location can be non-atomic? What does it mean for a single write to be non-atomic anyway? – Engin Kayraklioglu Mar 09 '14 at 03:54
  • my understanding is that x86 is fairly unique in having the regular load be atomic, as in synchronized across caches and not reordered during execution. – Arvid Mar 09 '14 at 04:00