Boost Equivalent for Windows Events
In windows c++ I can create a handle to event
Handle h = CreateEvent(...)
I can then set and reset that event
SetEvent开发者_JS百科(...) and ResetEvent(...)
Finally, I can OpenEvents using the command OpenEvent(...)
Is there a boost equivelent for events?
I think you need to use boost::mutex
, boost::unique_lock
, boost::condition_variable
and possibly bool
in order to imitate Events.
You actually might need sort of WaitForSingleObject
in order to wait for an event. Might be like this:
void wait_for_user_input()
{
boost::unique_lock<boost::mutex> lock(mut);
while(!data_ready)
{
cond.wait(lock);
}
process_user_input(); // it might be not necessary to hold mutex locked here!!!
// if so just add curly braces like this:
// void wait_for_user_input()
// {
// {
// boost::unique_lock<boost::mutex> lock(mut);
// while(!data_ready) { cond.wait(lock); }
// }
// process_user_input();
// }
}
The threadsafe Boost Signals2 library might be of use to you. The original Signals library was not thread-safe, but implemented a signals/slots framework which isn't too many miles away from the ideas of events. Since Signals2 is threadsafe, you should be able to utilize it for passing events between threads.
What you want is named_condition variable from the boost interprocess library. The main difference from windows events is that you must use them in conjunction with a named_mutex.
Note, you can use these boost interprocess primitives within a single process. I assume you need these because you are using OpenEvent (which implies sharing of the object by use of a name). If you can avoid using a name, then you can use the non-named variants from the boost thread library.
A boost lock (mutex) would do pretty much the same thing. You can wait on those.
精彩评论