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:
- x = initial value (zero for this discussion)
- The unit gives X increments and results to themselves: Results = x = 1
- 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:
- X = initial value = zero again
- To evaluate the unit A call x = x + 1 x + 1, checks the value of a (x) and adds 1.
- To evaluate the unit B call x = x + 1.x, checks the value of B x (still zero) and adds 1.
- The unit completes first (by fate) and x + 1 is the result of 1 (phase 3) x 1 now.
- 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:
- A 64 bit double X = 0
- The unit Axx1122334455667788 is first assigned 32 bits to X, except X with 0x1122334400000000.
- The unit B assigns the race to 0 and xxffeeddeddbbaa9988. By the moment, both 32 bit parts are updated and X is now = 0xffeeddeddbbaa9988.
- 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
Post a Comment