Intel® oneAPI Threading Building Blocks
Ask questions and share information about adding parallelism to your applications when using this threading library.

Events/signals

jogshy
New Contributor I
1,061 Views
Does TBB include OS-portable classes for events / signals like Win32's WaitForSingleObject() / WaitForMultipleObjects(), SetEvent(), etc? I need to sleep a thread until an event is raised.

thx
0 Kudos
1 Solution
Andrey_Marochko
New Contributor III
1,061 Views
TBB provides class condition_variable. Its interface is mostly compatible with that of C++11 cond var, and TBB injects it into the namespace std.

View solution in original post

0 Kudos
5 Replies
Andrey_Marochko
New Contributor III
1,062 Views
TBB provides class condition_variable. Its interface is mostly compatible with that of C++11 cond var, and TBB injects it into the namespace std.
0 Kudos
jogshy
New Contributor I
1,061 Views
Nice thx! Btw, any example about how to simulate the WaitForMultipleObjects, pls? I see on the class definition it's really a WaitforSingleObject equivalent.
0 Kudos
RafSchietekat
Valued Contributor III
1,061 Views
I don't know about any established patterns (anybody?), but you'll probably have to do some work on the source side to associate those "events" with a consumer, e.g., by posting something to a queue with a condition variable, like pushing instead of pulling. If the source isn't active, you'll have to run a thread to wait for events and translate them into events pushed onto the queue (notifying any consumers about their presence), which seems a bit annoying. Of course, when you're doing that, you might as well enqueue tasks instead, I suppose.

Further insights welcome!
0 Kudos
jogshy
New Contributor I
1,061 Views
Btw, I wanted this to wait for 3 events ( "any" condition ). A class like this one would help a lot:

struct Event
{
//....
};

class EventList
{
public:
void Add ( const Event &e );
{
m_stlEvts.push_back(e);
}

void WaitAll ( const size_t timeInMS ); //timeInMS to wait/timeout or 0 for infinite
int WaitAny ( const size_t timeInMS ); //return the event # in the list triggered, or -1 for timeout, <-1 on error
int WaitSingle ( const size_t eventID ); //eventID=event # in the list. Return 0 on trigger, -1 on timeour, <-1 if error

private:
std::vector m_stlEvt;
};

thx
0 Kudos
Alexey-Kukanov
Employee
1,061 Views
With a condition variable a thread waits for a signal sent through the variable. Signals can be sent at any given time under whatever logical condition(s) a program wants to use. The persistence of conditions should also be guaranteed by the program; condition variable is just a general waiting mechanism.

The basic schema to emulate WaitForMultipleObjects is the following:
- a waiting thread, when woken up, checks for all logical conditions it is interested in, and if any is not satisfied, itwaits again, otherwise proceeds;
- whenever any of the tracked conditions changes to true, the thread that made the change should notify all threads waiting on the condition variable.
0 Kudos
Reply