Thread learning notes (2) - mutually exclusive object
1)
The concept of mutually exclusive object: The mutual exclusive object is a data structure for system kernel maintenance, which guarantees access to individual threads.
The structure of the mutually exclusive object: includes a number of uses, a thread ID, a counter usage number means how many threads are toning the object, the thread ID refers to the ID of the thread maintained by the mutually exclusive object. The counter represents the current thread call The number of objects.
2)
Creation of mutually exclusive objects
Handle CreateMutex (LPSecurity_Attributes LPMUTEXATTRIBUTES, / / Security Bool Binitialowner, // Initial owner, if false, the initial no owner lpctstr lpname // set the name of the mutual exclusive object);
3) Get a mutually exclusive object
DWORD WaitForSingleObject (HANDLE hHandle, handle DWORD dwMilliseconds // mutex object // 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, The // IF DWMILLISECONDS IS INFINITE, The // Function's Time-Out Interval Never ELAPSES.
If the second parameter is 0, it is immediately returned if it is in the state of the test object, and if it is infinite, it has been tested the status until the signal is received.
4) Release the mutual exclusive object
If a thread has a mutually exclusive object, the mutex is released when the thread runs, and the other thread does not get the mutex, it is not possible to operate, and use the ReleaseMutex (HWND) operation.
Below is the code:
#include
DWORD WINAPI FUN1PRO (LPVOID LPPARETER); DWORD WINAPI FUN2PRO (LPVOID LPPARETER);
// int index = 1000; int Ticket = 1000; Handle Hmutex;
Void main () {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; transasemutemutex (hmutex);} rete 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; transasemutex (hmutex) } Return 0;}
There is still a problem here, I have never understood that the line 2 is alternately running after adding the SLEEP.
IF (Ticket> 0) {Sleep (1); cout << "Num Two Sale Ticket: --------------" << Ticket - << "Sale" << endl; } When you go to SLEEP, a thread runs multiple times before running the second thread personally thinks, adding the SLEEP, the time film is running out, so the next process is running