1

I have an object X that works in its own thread and sometimes signals with (std::condition_variable) some_cv.notify_all().
I also have another object Y that perfoms overlapped read from Windows named pipe. Then I need Y to wait for either notification from some_cv, or completion of overlapped operation.

How can I do this? One solution I came up with is following:
1) Assosiate an hEvent with overlapped operation.
2) Spawn a thread that will wait on this hEvent and call some_cv.notify_all(). Then Y should figure out why it was waken.

However, this solution needs a whole new thread with the only purpose of waiting for hEvent and "forwarding signal" to some_cv. Instead, it would be much better to perform read with ReadFileEx, provide completion routine to it, and call some_cv.notify_all directly from this routine.

The problem is that this completion routine will have a chance to run only when Y comes to "alertable state". And I have no idea whether waiting on std::condition_variable is alertable in Windows or not.

So is waiting on std::condition_variable alertable in Windows? If not, what other method would you suggest?

Dmitry J
  • 867
  • 7
  • 20
  • I'm having a hard time thinking of any scenario where you want to wait on some outside condition besides data being available on the pipe. I think some information is missing about why Y is waiting for notification beyond the overlapped IO. overlapped IO waits for IO... Why would you want to wait on IO AND an the sky being blue, the sky being blue doesn't make data available. Only data being available makes data available. – Christopher Pisz Jun 22 '17 at 23:12
  • @ChristopherPisz, One obvious reason is that signal from `X` might mean that `Y` should stop waiting for data. Another is that `Y` receives some "commands" from another process through the pipe, and signals from `X` represent another source of commands. – Dmitry J Jun 22 '17 at 23:20
  • Does [cppreference example](http://en.cppreference.com/w/cpp/thread/condition_variable) suffice your case? – Ripi2 Jun 22 '17 at 23:27
  • @Ripi2, There are no named pipes or overlapped I/O in that example, no waiting for multiple conditions. So I don't quite understand what do you mean. – Dmitry J Jun 22 '17 at 23:33
  • The idea is `Y` waiting or working based on some condition, no matter its job. The job itself almost sure may need some lock/mutex use so as to not affect other tasks. – Ripi2 Jun 22 '17 at 23:37
  • The way I understand it, std::condition_variable is the closest you'll get to a hEvent from the std library. It is very likely how it's implemented. – Michaël Roy Jun 23 '17 at 00:59
  • Why not drop the std::condition_variable and use a plain hEvent/WaitForSingleObject combo? – Michaël Roy Jun 23 '17 at 01:07
  • @MichaëlRoy, I can't change public interface of class `X`. Signaling with `condition_variable` is part of this interface. – Dmitry J Jun 23 '17 at 09:16

0 Answers0