Post by VenkatCan someone suggest me how can we implement WaitForMultipleObjects API of
windows in Linux.
Any way you want to.
Post by VenkatAs we all know WaitForMultipleObjects allows a thread to wait for multiple
synchronization objects at once. It can be set to return when any or all of
the objects become available.
Exactly. There are many different ways you could do this. Perhaps one
way is for each thread to create its own condition variable to block on and
register a pointer to its condition variable and the events it's interested
in on a linked list. When a thread signals an event, it traverses the linked
list and signals the appropriate condition variables.
This is just one way to do it. Generally the best way is to rearchitect
so that you don't need to use it. Usually the reasons you need
WaitForMultipleObjects just don't apply on Linux. For example, it's commonly
used with I/O operations that signal their completion with an event, but
Linux has no such I/O operations.
Also, due to the way the windows API works, certain jobs sometimes have
to be done by certain threads. So it's not easy to use a single event and a
pool, because the 'wrong' thread might get a task. But there are no such API
functions in Linux, so this doesn't apply.
Perhaps one solution would simply be to have one thread waiting on each
event. You shouldn't have that many events, and if you do, you probably
should just consolidate them. For example, instead of having a 'client X
needs service' event and a 'client Y needs service' event (thus a thread
might need to wait on both of them), just have a 'one or more clients need
service' event. A thread that detects that event can service any one client
that needs it.
DS