CreateEvent process synchronization

Keywords: C++ Attribute Windows

CreateEvent inter process synchronization

CreateEvent can create or open a named or unnamed event object.

HANDLE CreateEvent(
  LPSECURITY_ATTRIBUTES lpEventAttributes, // pointer to security attributes
  BOOL bManualReset, // flag for manual-reset event
  BOOL bInitialState, // flag for initial state
  LPCTSTR lpName // pointer to event-object name
);

If the     lpsecurity   attributes structure is used to create the access control attribute of an object, if it is NULL, the default security descriptor will be used, and the object can be inherited by a child process.

When the parameter     bManualReset is TRUE, the created object needs to manually call the ResetEvent function to restore to the non signal state (i.e. non responsive); if it is FALSE, the event object will automatically restore to the non signal state after responding to the waiting thread.

When the bInitialState parameter is TRUE, the initial state of the created object is signalstate (responsive); otherwise, it is non signalstate.

    lpName parameter is the name of event object, the length is not more than max  path, character sensitive; if it is NULL, an unnamed object is created.

Now test how to synchronize between processes.

#include "stdio.h"
#include "Windows.h"

int main() {

	HANDLE hEvtObj = NULL;
	DWORD hRet = NULL;
	CHAR objName[] = { "ObjTestEvt_123" };

	if (hEvtObj = CreateEventA(
		NULL, 
		TRUE,		// Manual reset to non signal state 
		FALSE,		// Initial non response 
		objName)) 
	{
		if (ERROR_ALREADY_EXISTS == GetLastError()) { // Event object already exists
			printf("Event Obj \"%s\" has EXISTED ...\n", objName);
			for (int i = 0; i < 10;) {			// Respond 10 times
				hRet = WaitForSingleObject(hEvtObj, 300);	// The timeout is 0.3 seconds
				if (!hRet) {
					printf("\"%s\" is now SIGNALED %d\n", objName, i++);	// Signal state
				}
				else {
					if (WAIT_TIMEOUT == hRet)
						printf("\"%s\" is now NONSIGNALED\n", objName);		// Non signal state
					else
						printf("Wait Error %#x...\n", GetLastError());
				}
				Sleep(1000);	// Sleep for a second
			}
		}
		else {	// Event object created successfully
			printf("Create Evt Obj \"%s\" Successful\n", objName);
			Sleep(1000);		// Wait for synchronization process to run
			printf("Set Event \"%s\" To SIGNALED  for 4 seconds...\n", objName);
			SetEvent(hEvtObj);		// Set event object as signal state
			Sleep(4000);			// Dormant 4s
			printf("Reset Event \"%s\" To NONSIGNALED for 4 seconds...\n", objName);
			ResetEvent(hEvtObj);	// Set to non signal state
			Sleep(4000);			// Leave the event object in non signal state for 4s
			printf("Set Event \"%s\" To SIGNALED...\n", objName);
			SetEvent(hEvtObj);		//
		}
		CloseHandle(hEvtObj);
		hEvtObj = NULL;
	}
	else {	// Create failure
		printf("CreateEvent Error = %#x", GetLastError());
	}

	return 0;
}

   in addition to interprocesses, threads can also use this method.

  reference: CreateEvent

Posted by Oaquasis on Sun, 19 Apr 2020 09:42:44 -0700