Do atomic operations perform the same processes as in threads?

Obviously, atomic operations guarantee that different threads will not compress the value. But is this still true for processes when shared memory is used? Even if the processes are planned by the OS to work on different cores? Or through different processors?

Edit: Also, if it's not safe, is it even unsafe on an operating system like Linux, where the processes and threads are the same from the scheduler’s point of view?

+3
source share
1 answer

tl; dr: . , . , , .

, , . , , , :

  • x = ( )
  • Entity A x : result = x = 1.
  • Entity B x : result = x = 2.

A B , .

- , .. , :

  • x = value = .
  • Entity A x = x + 1. x + 1 A x () 1.
  • Entity B x = x + 1. x + 1 B x ( ) 1.
  • B ( ) x + 1 = 1 ( 3) x. x 1.
  • Entity A x + 1 = 1 ( 2) x. x 1.

, B A .

, x 64- , . - :

  • 64- double x = 0.
  • A xxx1122334455667788. 32 , x 0x1122334400000000.
  • Entity B 0xffeeddccbbaa9988. , 32- , x = 0xffeeddccbbaa9988.
  • Entity A , x = 0xffeeddcc55667788.

, - .

+3

Source: https://habr.com/ru/post/1720402/


All Articles