The event object is like a switch: it has only two states---on and off. When an event is in the "on" state. We call it a "signal." Otherwise, it is called "no signal".
The ability to create an event object in a thread's run function. Then observe its state, assuming that the thread sleeps with "no signal". This way, the thread consumes less CPU time.
The function that produces the event object is as follows:
(1) CreateEvent
Function Prototypes:
HANDLE CreateEvent (
Lpsecurity_attributes lpeventattributes,//Security properties
BOOL bManualReset,//Reset mode
BOOL Binitialstate,//Initial state
LPCTSTR lpname//object name
);
The function creates an event synchronization object that, assuming the CreateEvent call succeeds, returns a handle to the newly generated object. Otherwise, NULL is returned.
Lpeventattributes: generally null
bManualReset: Specifies whether the event object is created to be manually restored or self-healing.
Assuming true (manual), the ResetEvent function must be used manually to restore the state of the event to a signal-free State, even if the thread is waiting for processing. Assuming that it is set to False (self-active), when the event is processed by a waiting thread and released, the system proactively reverts the event state to a signal-free state. So the two can be used for different occasions, their own initiative to reset the need for each event signal will be executed, and manual reset can be carried out.
Binitialstate: Specifies the initial state of the event object. assumed to be true. The initial state is signaled, otherwise there is no signal state.
Lpname: The name of the object that specifies the event, which may be used in the OpenEvent function.
Demo Sample code://Create a well-known. Cannot be inherited, manually restored, the initial state is an event object with no signal status:
Handle h = createevent (Null,true,false, "MyEvent");
Once an event is created, it is able to use the OpenEvent () API to get its handle, to close it with CloseHandle (), to set it to signal with setevent () or pulseevent (), and to use the ResetEvent () To make it signal-free, use WaitForSingleObject () or waitformultipleobjects () to wait for it to become signaled.
(2) SetEvent
Function Prototypes:
BOOL WINAPI SetEvent (
__in HANDLE hevent
);
Sets the state of the event to be marked.
Assuming that the event was created manually, this event will remain marked until resetevent is called.
Assuming that the event is self-active, this event will remain marked until a thread is freed and the system will set the event's status to Untagged.
(3) ResetEvent
Function Prototypes:
BOOL ResetEvent (
HANDLE hevent
);
Hevent a handle to the event object. Returned by the CreateEvent or OpenEvent function. This handle requires Event_modify_state access.
The function succeeds, returns a value other than 0, otherwise returns a value of 0 that can be called GetLastError to get the exact information of the error.
This function is used to manually reset the event object. Manually reset objects must be manually reset to a no-signal state after the thread is released. An event object that itself resets itself to a non-signaled state after a thread that waits for its success is released.
Resetting a non-signaled event object has no effect whatsoever.
(4) WaitForSingleObject
Function Prototypes:
DWORD WaitForSingleObject (
HANDLE Hhandle,
DWORD dwmilliseconds
);
The parameter hhandle is the handle of an event, and the second parameter dwmilliseconds is the time interval. Assume that the event is signaled and returns WAIT_OBJECT_0. Returns Wait_timeout if the event exceeds the Dwmilliseconds value but the event is not signaled. The front-line will always block the program at that statement.
Example:
#include <windows.h>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
// Learn from the event that there is another thread's internal state
// Example: There are three states inside the thread: get up, eat, go to work
HANDLE getupHandle;
HANDLE breakfastHandle;
HANDLE workHandle;
vector <string> stateTexts;
DWORD WINAPI Worker (LPVOID n);
int main ()
{
stateTexts.reserve (3); // The container reserves
getupHandle = CreateEvent (NULL, TRUE, FALSE, NULL); // Manually. no signal
breakfastHandle = CreateEvent (NULL, TRUE, FALSE, NULL);
workHandle = CreateEvent (NULL, TRUE, FALSE, NULL);
DWORD threadId;
HANDLE threadHandle = CreateThread (NULL,
0,
Worker, // thread entry function
0,
0,
& threadId);
WaitForSingleObject (getupHandle, INFINITE);
cout << stateTexts [0] << endl;
WaitForSingleObject (breakfastHandle, 3000);
cout << stateTexts [1] << endl;
WaitForSingleObject (workHandle, INFINITE);
cout << stateTexts [2] << endl;
CloseHandle (threadHandle);
CloseHandle (getupHandle);
CloseHandle (breakfastHandle);
CloseHandle (workHandle);
system ("pause");
}
DWORD WINAPI Worker (LPVOID n)
{
stateTexts.push_back ("GetUp");
SetEvent (getupHandle);
stateTexts.push_back ("Breakfast");
ResetEvent (breakfastHandle);
stateTexts.push_back ("Work");
SetEvent (workHandle);
return 0;
}
Execution Result:
Show GetUp first. Wait for 3000ms to show breakfast, then immediately show work
Because Getuphandle and Workhandle are set to signaled, their waitforsingleobject directly have a return value
Breakfasthandle is set to No signal, its WaitForSingleObject must wait for the value of the completion time of the return
Copyright notice: This article Bo Master original articles, blogs, without consent may not be reproduced.
CreateEvent, SetEvent, ResetEvent and WaitForSingleObject