multithreading - Do atomic operations work the same across processes as they do across threads? -


Obviously, atomic operations ensure that different threads do not reduce a single value. But when using shared memory it is also true in the processes? Even though the process is going to be determined by OS to run on different cores? Or in different CPUs?

EDIT: Besides, if it is not secure, then is it not even safe on operating systems like Linux, where processes and threads are similar from the scheduler's perspective?

tl; Dr: Some will be atoms by design but can travel on some variable types. In general, however, a nuclear operation maintains its contract between different processes, such as between threads.

An atomic operation really only ensures that you do not have an incompatible situation if two organizations are said together. For example, an atomic increment that two different threads or processes are called on the same integer will always behave like this:

  1. x = initial value (zero for this discussion)
  2. The unit gives X increments and results to themselves: Results = x = 1
  3. The edge of the unit B gives X and gives the result itself: Results = x = 2

Where A and B calls the first and second thread or process that calls.

Due to the condition of a non-nuclear operation breed, there may be incompatible or generally crazy results, for imperfect examples, you can easily see it:

  1. X = initial value = zero again
  2. To evaluate the unit A call x = x + 1 x + 1, checks the value of a (x) and adds 1.
  3. To evaluate the unit B call x = x + 1.x, checks the value of B x (still zero) and adds 1.
  4. The unit completes first (by fate) and x + 1 is the result of 1 (phase 3) x 1 now.
  5. The unit A destroys the other and specifies the result of x + 1 = 1 (phase 2) x for x is now 1.

Note the position of the race as ABB before ABB and completes the expression first.

Now imagine that x was a 64-bit double that is not sure to perform nuclear work. In that case you can easily see something like this:

  1. A 64 bit double X = 0
  2. The unit Axx1122334455667788 is first assigned 32 bits to X, except X with 0x1122334400000000.
  3. The unit B assigns the race to 0 and xxffeeddeddbbaa9988. By the moment, both 32 bit parts are updated and X is now = 0xffeeddeddbbaa9988.
  4. The unit completes its assignment with the second half and X is now = 0xffeeddcc55667788.

These non-nuclear works are some of the most disgusting concurrent bugs that you have to diagnose.


Comments

Popular posts from this blog

asp.net - Javascript/DOM Why is does my form not support submit()? -

sockets - Delphi: TTcpServer, connection reset when reading -

javascript - Classic ASP "ExecuteGlobal" statement acting differently on two servers -