How to wait for Callback response in C++ on Windows

207 Views Asked by At

I'm using an ExNotifyCallback function to signal from one component to another. There are several instances listening to this CB and only the proper one will respond. The response comes via a call through a separate function pointer interface:

void (*pfnResponseFunc)(void*pData, int* pDataSize)

I want to issue the ExNotifyCallback and then wait for a response coming through the pfnResponseFunc.

Code flow that would occur in a function:

// Send message to listeners:
ExNotifyCallback(myCbObject, pIn1, pIn2);

// Wait (with timeout) for pfnResponseFunc to get called...
if (responseFound)
{
   // read pData that showed up in pfnResponseFunc, return success
}
else
{
   // return failure
}

I want this to specifically wait for a fixed timeout in the same function. Is there a typical way to do this in windows?

1

There are 1 best solutions below

0
sam msft On

The basic solution has two parts:

  1. I think I normally would use an event for this, to signal between multiple components.

  2. Then, your components can wait for the event using WaitForSingleObject, which has a timeout in MS.

There is a lot of detail you are missing here though.

  • How do you want the signal to behave, ie. manual reest, pulse?
  • How will you share your event between processes? Is it a named event? Does one process duplicate the event handle to the other processes?
  • What is the threading model for the processes? Generally, I'd use the thread pool for this kind of stuff. There is TP wait which is good for waiting for events, and TP timers which you can use for timeouts...etc.

Anyway, there are many concurrency patterns you can use depending on what you want to do, but using events and the threadpool is the typical way of going about it.