Event kernel object Event


Notes on using the event CreateEventHANDLECreateEvent


(
LPSECURITY_ATTRIBUTESlpEventAttributes,// security attribute
BOOLbManualReset, // reset mode
BOOLbInitialState,                       // initial state
LPCTSTRlpName                           // object name
);
parameter
lpEventAttributes [input]
a pointer to the SECURITY_ATTRIBUTES structure to determine whether the returned handle can be inherited by child processes. If lpEventAttributes is NULL, the event will get a default security character.


bManualReset[input]
Specifies whether the event object is created to be manually restored or automatically restored. If TRUE, then the ResetEvent function must be used to manually reset the state of the event to the unsignaled state. If set to FALSE, when a waiting thread is released, the system will automatically restore the event state to no signal state.


bInitialState[input]
specifies the initial state of the event object. If TRUE, the initial state is signaled; otherwise, it is unsignaled.


lpName[input]
specifies the name of the event's object, a 0-terminated string pointer. The character format of the name is limited to MAX_PATH. Names are case sensitive.
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 time, since the bManualReset and bInitialState parameters have been set in the process of creating the event, these two parameters will be ignored. If the lpEventAttributes parameter is not NULL, it will determine whether this handle can be inherited, but its security descriptor members will be ignored.
If lpName is NULL, an unnamed event object will be created.
 
 
Note:
1. If you synchronize between processes, you need to create an event with a specific name, that is, the parameter lpName cannot be nil, and the name of the event object is the same.
2. In the case of manual restoration, first SetEvent initiates the signal, and then must use ResetEvent to restore to the no-signal state, one-to-one correspondence.


After SetEvent, all event objects have signals. When the event's object is set to the signaled state, any number of waiting threads, and subsequently threads that start waiting, are released.


  sample code

   1 Events and thread functions

/*  
Automatically reset objects:
  When the second parameter is FALSE, an automatic reset object is created. After triggering,
  Everyone is chaotic and scrambled, and only one thread can be scheduled;

To reset the object manually:
  When the second parameter is TRUE, create a manual reset object, after triggering,
  all threads get scheduled;
*/
//HANDLE g_hEvent=CreateEvent(NULL,FALSE,FALSE,NULL);

HANDLE g_hEvent=CreateEvent(NULL,TRUE,FALSE,NULL);

//Thread1
 UINT Thread1(LPVOID)
 {
	 AfxMessageBox("thread1  ready to run");
	 WaitForSingleObject(g_hEvent,INFINITE);
	 AfxMessageBox("thread1 finish");

	 return 1;

 }

 UINT Thread2(LPVOID)
 {
	 AfxMessageBox("thread2 read to run");
	 WaitForSingleObject(g_hEvent,INFINITE);
	 AfxMessageBox("thread2 finish");

	 return 1;

 }

 start thread code

        ResetEvent(g_hEvent);
	AfxBeginThread(Thread1,NULL);
	AfxBeginThread(Thread2,NULL);

 trigger event tag

       SetEvent (g_hEvent);
	::Sleep(100);

	ResetEvent(g_hEvent);




Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325889275&siteId=291194637