1) Concept of mutually exclusive object: Mutual exclusive object is a data structure for system kernel maintenance, which guarantees access to individual threads.
The structure of the mutual exclusive object: contains a number of usage, a thread ID, a counter
The number of use means how many threads are calling the object, and the thread ID refers to the ID of the thread that is maintained by the mutually exclusive object.
The counter represents the number of times the current thread call
2) Creation of mutually exclusive objects]
Handle Createmutex
LPSecurity_attributes lpMuteXAttributes, // security
Bool Binitialowner, // Initial owner, if False is initially no owner
LPCTSTR LPNAME / / Setting the name of the mutually exclusive object
);
3) Get a mutually exclusive object
DWORD WAITFORSINGLEOBJECT
Handle Hhandle, // Mutually exclusive handle
DWORD dwMilliseconds // Time-out interval, in milliseconds. // The function returns if the interval elapses, // even if the object's state is nonsignaled. // If dwMilliseconds is zero, the function tests the object's state and returns immediately. / / If dwmilliseconds is infinite, The Function's Time-Out Interval Never Elapses.
);
If the second parameter is 0, it is immediately returned immediately after the test object.
If it is infinite, the status is tested until the signal is accepted.
4) Release the mutual exclusive object
If a thread has a mutually exclusive object, then release the mutex after the thread is running, it is not possible to operate as other threads.
Use ReleaseMutex (HWND);
Below is the code
#include
#include
DWORD WINAPI Fun1Pro (LPVOID lpParameter); DWORD WINAPI Fun2Pro (LPVOID lpParameter); // int index = 1000; int ticket = 1000; HANDLE hMutex; void main () {HANDLE hThread1; HANDLE hThread2; hThread1 = CreateThread (NULL, 0, Fun1Pro, NULL, 0, NULL); hThread2 = CreateThread (NULL, 0, Fun2Pro, NULL, 0, NULL); CloseHandle (hThread1); CloseHandle (hThread2); hMutex = CreateMutex (NULL, FALSE, NULL); Sleep (4000 ); / * For (index; index> 0; index - {cout << "main thread run" << endl; // Sleep (10);} * /} DWORD WINAPI FUN1PRO (LPVOID LPPARETER) {// While (index> 0) // cout << "thread2 is run ................................. ......... "<< Endl; while (true) {WaitForsingleObject (hmutex, infinite); if (ticket> 0) {Sleep (1); cout <<" Num One Sale Ticket: << Ticket - << "Sale" << Endl;} else break; releasemutex (hmutex);} Return 0;} DWORD WINAPI FUN2PRO (LPVOID LPPARETER) {While (TRUE) {WaitForsingleObject (hmutex, infinite); if (Ticket> 0) {Sleep (1); cout << "Num Two Sale Ticket: --------------" << Ticket - << "Sale" << Endl;} else Break; ReleaseMutex (HMutex);} Return 0;} There is still a problem here.
Here, the line 2 is alternately running.
IF (Ticket> 0) {Sleep (1); cout << "Num Two Sale Ticket: --------------" << Ticket - << "Sale" << endl; }
When you remove the SLEEP, a thread runs multiple times before running the second thread.
Personally think that the time film is used up after plus SLEEP.
So the next process is then running