Recently looked at the Windows core programming, although is the older book, but the feeling is quite useful, incidentally reviewed some operating system knowledge, such as the process synchronization mechanism of this article
Thread synchronization in user mode
Communication is required between threads:
1. Multiple threads need to access one shared resource at the same time without destroying the integrity of the resource
2. One thread needs to notify other threads that a task has been completed
Method One: Atomic access: Interlocked series functions
InterlockedExchangeAdd (long & int);
InterlockedIncrement;
InterlockedExchange (it is useful to implement a rotary lock)
The variable addresses passed to these functions must be guaranteed to be aligned or the function may fail
The INTERLOCKDE function does not need to switch between kernel mode and user mode
Rotate Lock: Disable thread priority elevation
Multi-processor machine rotation Lock is more useful,
Key segment: Cycle a certain number of times, if still inaccessible, the thread switches to kernel mode until the resource is available
InterlockedCompareExchange
Method Two: Key segment
Critical_section G_cs;
EnterCriticalSection (&g_cs)
...
Leavecrititalsection (&g_cs)
Critical segments cannot synchronize threads between multiple processes
Method Three: Slim read/write lock
Similar to critical segments, but distinguish between read and write threads
Srwlock
Thread synchronization with kernel objects
There is a Boolean variable inside the process kernel object that initializes the value of this variable to false (not triggered) when the system creates the kernel object, and when the process terminates, the operating system sets the value in the corresponding kernel object to True, indicating that it has been triggered.
Wait function: WaitForSingleObject (Handle,dword)
WaitForMultipleObjects (Dword,const handle*, Bool,dword)
Event Kernel Object
Let one thread perform the initialization work, then start another thread and let it perform the rest of the work
CreateEvent ()
OpenEvent ()
SetEvent ()//when triggered
RESETEVETN ()
The counter kernel object that can wait
CreateWaitableTimer ()
Openwaitabletimer ()
SetWaitableTimer ()//when triggered
Signal Volume
Mutex Amount