Concurrency - Race conditions & data competition


         Not all race conditions are data competitions, and not all data competitions are race conditions, but both may make concurrent programs fail

1. Race conditions

        When the correctness of the calculation depends on the alternate execution timing of multiple threads, race conditions will occur.

        Like most concurrency errors, race conditions do not always cause problems, and improper execution timing is also required

The most common race conditions are:

  1. Detect before execution : The execution depends on the result of the detection, and the result of the detection depends on the execution timing of multiple threads, and the execution timing of multiple threads is usually not fixed and cannot be judged, which leads to various problems in the execution result.

        In computer memory or storage, race conditions may occur when instructions to read and write a large amount of data are issued at the same time. The machine tries to overwrite the same or existing data, while the old data is still being read. The result may be one or more of the following situations: the computer crashes, an illegal operation prompt appears and the program ends, the old data is read by mistake, or the new data is written by mistake. The serial memory and storage accesses can prevent this situation. When the read and write commands occur at the same time, the default is to perform the read operation first.

2. Data competition

        If synchronization is not used for collaboration when accessing shared non-final domains, data competition will occur. When a thread writes a variable and another thread reads this variable next, or reads a variable previously written by another thread, and synchronization is not used between the two threads, then data may appear competition.

        In the Java memory model, if there is a data race in the code, then this code may make the concurrent program fail.

3. Concurrent data competition (visibility) and race conditions (atomicity)

Data race:

In general data access, if a read process and a write process are not synchronized, then a data access error will occur.

Race condition:

If a change in the sequence of program execution will affect the final result, this is a race condition

Not all race conditions will have data competition. Race conditions sometimes depend on luck. Race conditions refer to objects that are not locked in the class. If there are no attribute variables in a class, it is called stateless, on the contrary it becomes stateful. For multiple processes, if the access sequence is correct, there will be no security issues such as data, but the access sequence of the processes is uncontrollable, and security errors are also common.

The third point is reference! ! ! , All processes are used in the quoting blog posts, but isn’t there no data sharing between processes? ? ? Leave doubts! No solution today! After a while to understand! Update again!

Guess you like

Origin blog.csdn.net/weixin_44556968/article/details/109983183