Event: Event events is actually an internal check. An event resembles the preceding semaphore, and an event has two states: the firing state and the non-excited state. Also known as signal status and no signal status.
There are two types of events: Manual reset events and automatic reset events. When a manual reset event is set to the firing state, all waiting threads are awakened, and remain in the firing state until the program resets it to the non-fired state. When the auto-reset event is set to the firing state, the "one" waiting thread is awakened, and then automatically reverts to the non-fired state.
An automatic event can be abstracted into four operations:
-Create CreateEvent (null,false,true,null);
-With Trigger WaitForSingleObject (G_hevent, INFINITE);
-Reset Activation SetEvent (g_hevent);
-Destruction of CloseHandle (g_hevent);
Manual event can be abstracted as five operations:
-Create CreateEvent (null,true,true,null);
-With Trigger WaitForSingleObject (G_hevent, INFINITE);
-Reset Inactive resetevent (g_hevent);
-Reset Activation SetEvent (g_hevent);
-Destruction of CloseHandle (g_hevent);
Function parsing:
1.
function function Description: Create or open a named or Nameless event object
Function Prototypes:
HANDLE CreateEvent
(
Lpsecurity_attributes lpeventattributes,//Security properties
BOOL bManualReset,//Reset mode
BOOL Binitialstate,//Initial state
LPCTSTR lpname//object name
);
Parameters:
Lpeventattributes:
A pointer to the SECURITY_ATTRIBUTES structure that determines whether the returned handle can be inherited by the quilt process. If lpeventattributes is null, this handle cannot be inherited. If
Lpeventattributes is null, and the event obtains a default security character.
bManualReset:
Specifies whether the event object is created to be manually restored or automatically restored. If true, you must use the ResetEvent function to manually restore the state of the event to a signal-free state. If set to False, when an event is released by a waiting thread, the system automatically reverts the event state to a no-signal state.
Binitialstate:
Specifies the initial state of the event object. If true, the initial state is signaled, otherwise there is no signal state.
Lpname:
The name of the object that specifies the event, which is a string pointer that ends with 0. The character format of the name is limited to MAX_PATH. The name is case-sensitive.
Attention:
1. If the name specified by Lpname is the same as the name of an existing named event object, the function will request event_all_access to access the existing object. At this point, because the bManualReset and binitialstate parameters are already set in the process of creating the event, these two parameters are ignored. If Lpeventattributes is a parameter that is not NULL, it determines whether the handle can be inherited, but its security descriptor members are ignored.
2. If lpname is null, an unnamed event object is created.
3. If the lpname is the same as an existing signal, mutex, wait timer, job, or file mapping object name, the function will fail and the Error_invalid_handle will be returned in the GetLastError function. This behavior is caused by the fact that these objects share a single namespace.
Note the point:
The difference between manual and automatic:
Automatic Reset: After SetEvent, the event is automatically reset to the non-triggered state.
Manual reset: After SetEvent, it is necessary to call the ResetEvent event before it is set to the non-triggering state.
Difference: When a manual reset event is triggered, all threads that are waiting for the event become scheduled; When an automatic reset event is triggered, the
Only one thread that is waiting for the event becomes a scheduled state. The system does not guarantee which of these threads will be dispatched, and the remaining threads will continue to wait. This allows the setevent to be called before each thread function returns
Other words:
(1) For manual set-up events, all the threads that are in the waiting state become scheduled.
(2) for automatic set-up events, all the threads that are in the waiting state are only able to become scheduled.
2.
Function Prototypes:
DWORD WaitForSingleObject
(
HANDLE Hhandle,
DWORD dwmilliseconds
);
Parameter resolution:
The parameter hhandle is a handle to an event, and the second parameter, Dwmilliseconds, is the time interval (INFINITE permanent). If the time is signaled the state returns.
The WaitForSingleObject function is used to detect the signal state of the Hhandle event. If Dwmilliseconds is a finite event, it returns when the function executes longer than dwmilliseconds.
However, if the parameter dwmilliseconds is infinite, the function will not return until the corresponding time event becomes signaled, otherwise it will wait until the WaitForSingleObject has returned straight to execute the following code.
Attention:
(1) If it is an auto-set event, then after each waitforsingleobject, the state becomes an inactive state and is activated with SetEvent ().
(2) If it is a manual set-up event, the original state will not be changed after each waitforsingleobject. To use the resetevent () setting to an inactive state, use SetEvent () to activate.
3.
SetEvent
function function: Trigger event
Function prototype: BOOL SetEvent (handlehevent);
Function Description: After each trigger, there must be one or more threads in the waiting state to become a scheduled state.
4.
ResetEvent
function function: Set event to end trigger
Function prototype: BOOL resetevent (handlehevent);
Source code://Semaphore.cpp: Defines the entry point of the console application. //#include"stdafx.h"#include<Windows.h>#include<process.h>//Number of Threads#defineG_nthreadnum 5//Signal VolumeHANDLE g_hevent;//Cumulative NumberintG_ncount =0; unsigned _stdcall threadfunc (void*LParam) { for(inti =0; I <100000; ++i) {//wait for the event to be triggered and then turn into no signalWaitForSingleObject (g_hevent, INFINITE); G_ncount++; //Reset event is activeSetEvent (g_hevent); } return 0;}int_tmain (intARGC, _tchar*argv[]) { //Create EventG_hevent= (HANDLE) CreateEvent (NULL,false,true, NULL); //Start ThreadHANDLE Pthread[g_nthreadnum]; for(inti =0; i < G_nthreadnum; ++i) {Pthread[i]= (HANDLE) _beginthreadex (NULL,0, ThreadFunc, NULL,0,0); if(Pthread[i] = =0) { Continue; I--; } } //wait for thread to endwaitformultipleobjects (G_nthreadnum, PThread, TRUE, INFINITE); printf ("g_ncount:%d\n", G_ncount); //Freeing Resources for(inti =0; i < G_nthreadnum; ++i) {CloseHandle (pthread[i]); } closehandle (G_hevent); GetChar (); return 0;}
Event of thread synchronization