This article mainly describes several different solutions to ensure thread synchronization and data security.
Take the lock as an example:
1. User-mode is much faster than kernel-mode.
2. the Windows operating system cannot find the user mode lock (even if it has been blocked), so the thread pool will not create a new thread to process new requests.
3. The lock in the core mode can stop the thread running, so that the CPU is not wasted, but the memory is wasted (when the conditions are met, Windows will wake up the previously dormant thread to continue execution)
4. User Mode Locking. In fact, the thread is still running, wasting CPU and memory.
5. The lock in the core mode will greatly damage the performance.
PS: the permanent locking of the user mode is called livelock, and the permanent locking of the core mode is called deadlock.
Considering the above situations
The user mode should be given priority when the lock time is short.
The core mode should be given priority when the lock takes a long time, because it will not waste CPU while waiting (I personally think that all those waiting for more than 1 ms should use the core Mode)
FCL has two native user mode locks: (we strongly recommend that you use multiple threads.ProgramApplying interlocked)
1. Volatile: marking a simple data type as volatile will make the read/write operations of this variable atomic (do not mark all the variables as volatile for convenience)
2. interlocked. This class encapsulates some operations for simple data types, such as exchange, compareexchange, and add (this class is very useful)
PS: I don't understand why interlocked does not support simple data types such as Boolean ......
Noun Translation:
User-Mode)
Kernel-mode)