Recently read the Win32 multithreaded programming, summed up a few
The main problem with multithreading is thread synchronization, and thread invocation is implemented by the operating system, so thread synchronization must be supported by the operating system, which mentions the busy loop and the sleep structure I used before to achieve thread synchronization, when I felt like there was no other way, Until you meet the Wait series API, here's a summary of the various thread synchronization methods
1. The use of critical areas, the advantages do not need to produce system kernel objects, fast, high efficiency. Key API:
void InitializeCriticalSection (Lpcritical_section lpcriticalsection)//Initialize critical section
void EnterCriticalSection (Lpcritical_section lpcriticalsection)//Enter critical section
void LeaveCriticalSection (Lpcritical_section lpcriticalsection)//Exit critical section
void DeleteCriticalSection (Lpcritical_section lpcriticalsection)//release Critical Zone resources
2. Mutex, fixed a deadlock problem that cannot be solved by critical section, resulting in kernel object. Because the kernel object can be shared by multiple processes, there is no OVA with the critical API
CreateMutex WaitForSingleObject Waitformultipleobject
3. The semaphore solves the problem that a resource cannot be shared by multiple threads at the same time. Parent Set key API that can be considered a mutex
CreateSemaphore
The 4 event, which can be used by the thread itself to change the excitation state, can be utilized to design its own synchronization mechanism key API
CreateEvent
Other
Read/write control lock, interlockedincrement for a single variable, etc.
The shortcoming of the book is obvious. Not deep enough, probably only the API call, the deep-seated principle of the introduction of a few, for the development of the application is enough, to the reverse of the staff, the understanding is almost.
Still a bit wordy, to say the truth!
Win32 Multithreading Design Summary