About hyper-threading
CPU hyper-threading simply means that when the cache miss of a thread needs to perform the memory access operation, the CPU will wait during the memory access, and then switch to another thread for execution to avoid wasting CPU resources. Memory Access is slow, and the hyper-threading switching design is fast. It is claimed that only one CPU cycle is required.
Why is hyper-threading so fast? Normal thread switching is slow because multiple threads use one thing, such as registers. When a normal thread is switched, the value of one thread must be switched to the value of another thread. Hyper-threading is independent of many things (everything that can be seen by software is independent of every hyper-threading, something that cannot be seen by software, such as caching (hardware-only things, the software cannot be controlled.) whether the design is independent is only known by the CPU designer .), Therefore, hyper-threading switching is fast. You can use another hyper-threading tool without switching the internal data of the register.
C language keyword volatile
The compiler will optimize the compilation of the Code. According to the code context, if the value of a variable is not changed, the value in the register will be directly used next time without memory access.
However, if two processes share one memory, process a changes, process B reads, and process B, they may not be able to read the correct data until they use the values in the register.
In this case, you need to add the volatile keyword when writing the code, telling the compiler not to optimize it, directly accessing the memory instead of taking the register value.
But access to memory is not necessarily a real access to the memory. It is possible that the access value is directly from the cache, And the cache cannot be controlled by the software (the Code cannot be specified to read in the memory or in the cache ), it is controlled by hardware.
Do not think that volatile will be forced to read from the memory, but tell the compiler not to optimize it into a read register, but to access the memory, but to access the memory or cache, it's about the running hardware.