The focus is on simultaneous access of multiple threads, keeping threads synchronized.
Problems with thread synchronization:
1, thread synchronization is cumbersome, and easy to write wrong.
2, thread synchronization can compromise performance, and it takes time to get and release a lock.
3, thread synchronization allows only one thread to access resources at a time.
class Library and thread safety ,
A thread-safe postback means that the data is not corrupted when two threads attempt to access the data at the same time.
Primitive user-mode and kernel-mode constructs
Primitives: Refers to the simplest constructs in code, with two primitive constructs: User mode and kernel mode.
1, the primitive user mode is faster than the primitive kernel mode because of the direct use of special CPU instructions to coordinate threads that occur in hardware.
2, primitive user mode constructs the disadvantage that only the Windows operating system kernel can stop a thread from running. A running thread in user mode may be preempted, but it will be dispatched at the fastest speed, resulting in a resource being taken, but temporarily leaving the program running in user mode, wasting time.
3, primitive kernel-mode construction: The Windows operating system itself provides, and application threads invoke functions implemented in the system kernel. Switching threads from user mode to kernel mode (or vice versa) can cause significant performance loss. Advantage: The calling thread will block the thread, but it will not waste CPU time.
3, Hybrid construction: Combining primitive user mode and primitive kernel mode advantage
User mode constructs:
Divided into two primitive user-mode thread synchronization constructs: volatile structures and interlocking constructs
Volatile structure: contains atomic or read or write operations on a simple data type
interlocking constructs: Variables that are contained in a simple data type perform atomic read and write operations
CLR Via C # reading notes----primitive thread synchronization constructs