rossb83
rossb83

Reputation: 1762

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(...) and ResetEvent(...)

Finally, I can OpenEvents using the command OpenEvent(...)

Is there a boost equivelent for events?

Upvotes: 7

Views: 9164

Answers (4)

csj
csj

Reputation: 22416

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.

Upvotes: 4

user184968
user184968

Reputation:

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();
                          // }



}

Upvotes: 6

zdan
zdan

Reputation: 29450

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.

Upvotes: 0

T.E.D.
T.E.D.

Reputation: 44804

A boost lock (mutex) would do pretty much the same thing. You can wait on those.

Upvotes: -1

Related Questions