Windows Thread Synchronization 32

 

 

 

Asynchronous Example

 

The g_InitOnce global variable is the one-time initialization structure. The OpenEventHandleAsync() function returns a handle to an event that is created only once, or INVALID_HANDLE_VALUE. OpenEventHandleAsync calls the InitOnceBeginInitialize() function to enter the initializing state. If the call succeeds, the code checks the value of the fPending parameter to determine whether to create the event or simply return a handle to the event created by another thread. If fPending is FALSE, initialization has already completed so OpenEventHandleAsync() returns the event handle returned in the lpContext parameter. Otherwise, it calls the CreateEvent() function to create the event and the InitOnceComplete() function to complete the initialization. If the call to InitOnceComplete() succeeds, OpenEventHandleAsync() returns the new event handle. Otherwise, it closes the event handle and calls InitOnceBeginInitialize with INIT_ONCE_CHECK_ONLY to determine whether initialization failed or was completed by another thread. If the initialization was completed by another thread, OpenEventHandleAsync() returns the event handle returned in lpContext. Otherwise, it returns INVALID_HANDLE_VALUE.

Create a new empty Win32 console application project. Give a suitable project name and change the project location if needed.

 

The Asynchronous Example: Creating new C++ Win32 empty console application Visual C++ project

 

 

Then, add the source file and give it a suitable name.

 

The Asynchronous Program Example: Adding new C++ source file to the existing project

 

Next, add the following source code.

 

// Min Vista/server 2008

// https://msdn.microsoft.com/en-us/library/aa383745%28VS.85%29.aspx

#define _WIN32_WINNT 0x0600

#include <windows.h>

#include <stdio.h>

 

// Global variable

INIT_ONCE g_InitOnce;

 

HANDLE OpenEventHandleAsync()

{

  PVOID  lpContext;

  BOOL   fStatus;

  BOOL   fPending;

  HANDLE hEvent;

 

  fStatus = InitOnceBeginInitialize(&g_InitOnce, INIT_ONCE_ASYNC, &fPending, &lpContext); 

 

  if (!fStatus)

  {

        wprintf(LInitOnceBeginInitialize() with INIT_ONCE_ASYNC failed!\n);

        return (INVALID_HANDLE_VALUE);

  }

  else

        wprintf(LInitOnceBeginInitialize() with INIT_ONCE_ASYNC specified should be fine!\n);

 

  // Initialization has already completed.

  if (!fPending)

  {

        wprintf(LInitOnceBeginInitialize() with INIT_ONCE_ASYNC specified failed!\n);

        return (HANDLE)lpContext;

  }

  else

        wprintf(LInitialization has already completed...\n);

 

  hEvent = CreateEvent(NULL, TRUE, TRUE, NULL);

 

  if (hEvent == NULL)

  {

        wprintf(LCreateEvent() failed, error %u\n, GetLastError());

        return (INVALID_HANDLE_VALUE);

  }

  else

        wprintf(LCreateEvent() is OK!\n);

 

  fStatus = InitOnceComplete(&g_InitOnce,  INIT_ONCE_ASYNC,  (PVOID)hEvent);

 

  if (fStatus)

  {

        wprintf(LInitOnceComplete() should be fine!\n);

        return hEvent;

  }

  else

        wprintf(LInitOnceComplete() failed, error %u!\n, GetLastError());

 

  // Initialization has already completed. Free the local event.

  CloseHandle(hEvent);

 

 

  // Retrieve the final context data.

  fStatus = InitOnceBeginInitialize(&g_InitOnce, INIT_ONCE_CHECK_ONLY, &fPending, &lpContext);

 

  if (fStatus && !fPending)

  {

        wprintf(LInitOnceBeginInitialize() with INIT_ONCE_CHECK_ONLY specified & !fPending is OK!\n);

        return (HANDLE)lpContext;

  }

  else

  {

        wprintf(LInitOnceBeginInitialize() with INIT_ONCE_CHECK_ONLY failed!\n);

        return INVALID_HANDLE_VALUE;

  }

}

 

int wmain()

{

      OpenEventHandleAsync();

      return 0;

}

 

Build and run the project. The following screenshot is a sample output when run on Windows XP Pro SP2.

 

The Asynchronous Program Example: A sample output when running the example on Windows XP. It should be run on Windows Vista/Server 2008 and above

 

 

 

< Thread Synchronization 31 | Thread Synchronization Programming | Win32 Programming | Thread Synchronization 33 >