This chapter describes the other: event objects and key code snippets.
Process-related functions:
CreateEvent function:
First parameter: Security property, default security property is null
Second parameter: Reset mode, manually set to True, automatically set to False,
When the thread waiting for the event is set to manual, the ResetEvent function is required to set it to a no-model state.
Third parameter: Initial state: True for signaled state, false for no signal status.
Fourth parameter: Object name, NULL is an anonymous name.
Create or open a named or Anonymous event object (also part of a kernel object)
Return: The handle of the event object is returned.
SetEvent function:
Sets the specified event object to a signaled state
ResetEvent function:
Sets the specified signal to a signal-free state.
1. Event object:
The event object also belongs to the kernel object,
Contains
(1) Usage count
(2) Whether to reset the object automatically or manually reset the object's bool value
(3) Whether the object is in a notification or is not a notified bool value.
Here is an example of thread synchronization using an event object:
The following in the main thread of the main function, call the CreateEvent function Create a manual reset event kernel object, the second argument is true, and the 3rd argument is False
So the initial time is no signal state, so that other threads call WaitForSingleObject when the event object is not. So you need to call the SetEvent function
Set the event to a signaled state.
In a child thread, the call to the WaitForSingleObject function can get this event object, but at the same time other threads can, so you need the ResetEvent function to set the event object
Set to no signal state, then call the SetEvent function after executing the action to set its event object to signaled state.
1#include <Windows.h>2#include <iostream>3 using namespacestd;4 5 inttickets= -;6 HANDLE g_hevents;7 8 DWORD WINAPI fun1 (lpvoid lpparmeters)9 {Ten while(TRUE) One { AWaitForSingleObject (G_hevents,infinite);//Request Object Event -ResetEvent (g_hevents);//set event object to no signal state - if(tickets>0) the { -Sleep (1); -cout<<"Thread1 Sell Tickets:"<<tickets--<<Endl; -SetEvent (g_hevents);//sets the event object to a signaled state. + } - Else + { ASetEvent (g_hevents);//sets the event object to a signaled state. at Break; - } - } - return 0; - } - DWORD WINAPI fun2 (lpvoid lpparmeters) in { - while(TRUE) to { +WaitForSingleObject (G_hevents,infinite);//Request Object Event -ResetEvent (g_hevents);//set event object to no signal state the if(tickets>0) * { $Sleep (1);Panax Notoginsengcout<<"Thread2 Sell Tickets:"<<tickets--<<Endl; -SetEvent (g_hevents);//To set the event object to a signaled state the } + Else A { the Break; +SetEvent (g_hevents);//To set the event object to a signaled state - } $ } $ return 0; - } - the - intMain ()Wuyi { the HANDLE Thread1; - HANDLE thread2; Wu - //The thread-safe property is null by default, manually set the signal state to true, set to no signal state false, About //event name Null anonymous. $g_hevents=Createeventa (null,true,false,null); -SetEvent (g_hevents);//To set the event object to a signaled state - -Thread1=createthread (NULL,0, Fun1,null,0, NULL); AThread2=createthread (NULL,0, Fun2,null,0, NULL); + CloseHandle (thread1); the CloseHandle (thread2); - $Sleep (4000); the CloseHandle (g_hevents); theSystem"Pause"); the the}
VC + + in-depth--16 chapter: Thread synchronization, Event object