About events
Events (event) are the most flexible way of synchronizing threads provided by WIN32, and events can be in an excited state (signaled or true) or not fired (unsignal or false). Depending on the mode of state transition, events can be divided into two categories:
(1) Manual settings: This object can only be manually set by the program, when this event is needed or events occur, the use of SetEvent and resetevent to set.
(2) Automatic recovery: Once the event occurs and is processed, automatically revert to no event state, do not need to set again.
The function prototype for creating the event is:HANDLE CreateEvent (
Lpsecurity_attributes Lpeventattributes,
Security_attributes the structure pointer, which can be null
BOOL bManualReset,
Manual/Automatic
TRUE: The ResetEvent purge signal must be manually invoked after WaitForSingleObject
FALSE: The system automatically clears the event signal after WaitForSingleObject
BOOL Binitialstate,//Initial state
LPCTSTR lpname//Event name
);
The
Use the event mechanism should be aware of the following:
(1) If you are accessing events across processes, you must name the event, and when naming the event, be careful not to conflict with other global named objects in the System namespace;
(2) event to be automatically restored;
(3) The initial state setting of the event.
Look at the following code:
DWORD WINAPI ThreadProc (LPVOID lpparam);
DWORD WINAPI ThreadProc2 (LPVOID lpparam);
DWORD G_dwthreadid;
DWORD G_dwthreadid2; UINT g_ntickets = 300; int g_ntickets = 300;
Remark 1 HANDLE g_hevent = NULL;
HANDLE g_hevent1 = NULL;
HANDLE g_hevent2 = NULL;
Critical_section G_cs;
int threadcout = 0;
int main () {cout << "main thread is running." << Endl; InitializeCriticalSection (&G_CS)//Initialization critical section HANDLE Hhandle = CreateThread (null, 0, threadproc, NULL, 0, &g_dwthre
ADID);
threadcout++;
HANDLE hHandle2 = CreateThread (null, 0, THREADPROC2, NULL, 0, &G_DWTHREADID2);
threadcout++;
G_hevent = CreateEvent (null, FALSE, TRUE, NULL); G_hevent1 = CreateEvent (null, FALSE, TRUE, NULL);
Note 5:g_hevent1 = CreateEvent (null, True, true, null); G_hevent2 = CreateEvent (null, FALSE, TRUE, NULL);
Note 5:g_hevent2 = CreateEvent (null, True, true, null);
ResetEvent (G_HEVENT1);
ResetEvent (G_hevent2);
SetEvent (G_HEVENT1);
while (TRUE){entercriticalsection (&G_CS);
int ncount = Threadcout;
LeaveCriticalSection (&g_cs);
if (ncount = = 0) {cout << "Main thread is a break." << Endl;
Break } sleep (1000);
Remark 4 CloseHandle (hhandle);
CloseHandle (HHandle2);
DeleteCriticalSection (&g_cs);
cout << "Main thread is end." << Endl;
System ("pause");
return 0; DWORD WINAPI ThreadProc (lpvoid lpparam) {//cout << "No." << g_dwthreadid << "Thread is run
Ning. "<< Endl;
while (TRUE) {WaitForSingleObject (g_hevent1, INFINITE);
cout << "No.1" << g_dwthreadid << "thread is running." << Endl;
EnterCriticalSection (&g_cs);
int temp= g_ntickets;
LeaveCriticalSection (&g_cs);
cout << "No.1" << g_dwthreadid << "Thread is temp." << Endl; if (Temp > 0) {sleep (10); Sleep (1000)//Memo 2 cout << "no.1-" << G_dwthreAdid << "Sell ticket:" << temp << Endl;
EnterCriticalSection (&g_cs);
g_ntickets--;
LeaveCriticalSection (&g_cs);
SetEvent (G_hevent2);
ResetEvent (G_HEVENT1)//remark 6} else {cout << "no.1-break" << Endl;
ResetEvent (G_HEVENT1)//Memo 6 SetEvent (G_HEVENT2);//Without this THREADPROC2 cannot terminate//remark 3 break;
} entercriticalsection (&g_cs);
threadcout--;
LeaveCriticalSection (&g_cs);
cout << "No.1-end" << Endl;
return 0;
DWORD WINAPI ThreadProc2 (lpvoid lpparam) {/while (TRUE) {WaitForSingleObject (G_hevent2, INFINITE);
cout << "No.2" << g_dwthreadid2 << "thread is running." << Endl;
EnterCriticalSection (&g_cs);
int temp= g_ntickets;
LeaveCriticalSection (&g_cs); if (Temp > 0) {sleep (10); Sleep (1000)//Memo 2 cout << "no.2-" << g_dwthreadid2 << "sell ticket:" << temp << Endl;
EnterCriticalSection (&g_cs);
g_ntickets--;
LeaveCriticalSection (&g_cs);
SetEvent (G_HEVENT1);
ResetEvent (G_HEVENT2)//remark 6} else {cout << "no.2-break" << Endl;
ResetEvent (G_HEVENT2)//Memo 6 SetEvent (G_HEVENT1);//The same problem, no this threadproc cannot terminate//remark 3 break;
} entercriticalsection (&g_cs);
threadcout--;
LeaveCriticalSection (&g_cs);
cout << "No.2-end" << Endl;
return 0; }
This code is connected to the uncertainty problem of the uint type as a cyclic variable continues to improve, adding a critical section to control the access of global variables. This article will explain the use of SetEvent and resetevent, which depends on Note 5 and note 6. Note 5: CreateEvent's second argument determines whether a manual call to ResetEvent is required, and when true, it needs to be invoked manually, if not invoked. Without invocation, the event will be in a signaled state, that is, note 6. When false, you do not need to call manually, the call does not call, the effect is the same. It is also good practice to put resetevent in front of the WaitForSingleObject.
Reprint please indicate original link: http://blog.csdn.net/wujunokay/article/details/12272581