7

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?

Sam Hosseini
  • 813
  • 2
  • 9
  • 17
rossb83
  • 1,694
  • 4
  • 21
  • 40
  • Do you require the multi-process IPC aspect of `OpenEvent`, or are you only needing within-process communication? – Thanatos Apr 08 '11 at 17:53
  • Even though not strictly a duplicate, this http://stackoverflow.com/questions/1677070/cross-platform-equivalent-to-windows-events question provides the proper answer to your question. – Cray Nov 25 '12 at 22:15

4 Answers4

6

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



}
4

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.

gbjbaanb
  • 51,617
  • 12
  • 104
  • 148
csj
  • 21,818
  • 2
  • 20
  • 26
0

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.

zdan
  • 28,667
  • 7
  • 60
  • 71
  • 3
    IPC is not required according to the author. Besides `name mutexes` if an application crashes are not removed automaticly. And as a result after one adds named mutexes he or she also has to handle somehow this sitution. IMHO it just create unneccessary complexity. –  Apr 08 '11 at 18:25
  • It's not required according to the author, but that part of the question is buried in a comment below the question. This is the right answer without that comment. – aggieNick02 Oct 19 '18 at 18:35
-1

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

T.E.D.
  • 44,016
  • 10
  • 73
  • 134
  • Not necessarily. I would like to run a while loop in thread A continuously until thread B triggers an event. – rossb83 Apr 08 '11 at 17:56
  • so use "timed_lock(mutex, timeout)" inside of your loop – J T Apr 08 '11 at 18:03
  • I don't want any timings coming into play. I simply want an event trigger. When user clicks a button that causes an event. – rossb83 Apr 08 '11 at 18:10