Race: The phenomenon that the correctness of a calculation depends on time is called race

Interpretation of race results: two-dimensional table analysis

Race mode: read-modify-write, check-then-act

Thread-safe: A class that works well in a single-threaded environment is thread-safe if it also works well in a multi-threaded environment without making any changes to it

Thread safety issues can be summarized in three aspects: atomicity, visibility and order

Atomicity: An access operation to a shared variable is called an atomic operation if the operation is indivisible from any thread other than its executing thread. Atomic operation + atomic operation! = atomic operation)

Visibility: When a thread makes an update to a shared variable, subsequent threads accessing the variable may not be able to read the update immediately or ever; Visibility is usually related to the storage system, the process of updating variables by threads: variables are stored in the register of the thread used — write buffer — cache — main memory; When other processors are notified, they may simply store the contents of the update notification in an invalidation queue; Therefore, the multi-thread operation of shared variables is to move the shared variables from the main memory to their respective registers, modify the value by writing buffer, cache, and then back to the main memory; All components other than main memory are referred to as processor caches

Orderliness: Memory access operations performed by one thread running on one processor appear to be out of order by other threads running on another processor; Out-of-order: The order of memory access operations appears to have changed, involving the concept of reordering: instruction reordering (source compiler, processor), storage subsystem reordering (source cache, write buffer)

Ensure sequential access to memory: The logical part of this disables reordering by invoking the corresponding instructions provided by the processor, memory barriers, which are explained in more detail in Chapter 3

Context switch: The product of multiple threads sharing the same processor

Thread activity failure: deadlock (two dogs fight) lock (prince saves princess, prince is dead) live lock (cat bites tail) hungry

For more content, please pay attention to wechat public account: