Discussion:
How to wait on multiple condition variables or implement a similar semantics
(too old to reply)
vikky@gmail.com
2005-03-16 02:32:02 UTC
Permalink
Hi ,
I am porting an application from windows to linux. It uses function
WaitForMultipleObjects() to wait on multiple events and on the basis of
what event is signalled (which is specified by the functions return
value) it takes furthur action. The function is actually being used
here to wait for only two events,in mode where it returns if any of the
event is signalled. The first one being any event while the second one
always being the event signalled when the application is closing.
I am using pthreads in linux to port the application but i got stuck on
how to implement WaitForMultipleObjects() semantics. The closest
analogue i found to WaitForSingleObject() is using condition variables
and waiting on them. But alas i can't find any function in pthread
library that can wait on multiple condition varibles. I want to ask how
a suitable replacement for WaitForMultipleObjects() can be made.

Thanking you in advance,
Vikky
Joe Seigh
2005-03-16 03:30:52 UTC
Permalink
Post by ***@gmail.com
Hi ,
I am porting an application from windows to linux. It uses function
WaitForMultipleObjects() to wait on multiple events and on the basis of
what event is signalled (which is specified by the functions return
value) it takes furthur action. The function is actually being used
here to wait for only two events,in mode where it returns if any of the
event is signalled. The first one being any event while the second one
always being the event signalled when the application is closing.
I am using pthreads in linux to port the application but i got stuck on
how to implement WaitForMultipleObjects() semantics. The closest
analogue i found to WaitForSingleObject() is using condition variables
and waiting on them. But alas i can't find any function in pthread
library that can wait on multiple condition varibles. I want to ask how
a suitable replacement for WaitForMultipleObjects() can be made.
You have to use the same condition variable.

For bWaitAll == true

while (!(cond1 && cond2 && ...))
pthread_cond_wait(&cvar, &mutex);
For bWaitAll == false

while (!(cond1 || cond2 || ...))
pthread_cond_wait(&cvar, &mutex);

Use broadcast instead of signal if you have multiple waiters.

There are emulations of the windows WaitForMultipleObjects out
there, but they're pretty inefficient. So is the windows
implementation but that's hidden from you.
--
Joe Seigh
Pavel Lebedinsky
2005-03-16 04:58:39 UTC
Permalink
Post by Joe Seigh
There are emulations of the windows WaitForMultipleObjects out
there, but they're pretty inefficient.
So is the windows implementation but that's hidden from you.
Inefficient compared to what?

If you have a bunch of objects that you want to wait for
then I would expect it to be more efficient to use
WaitForMultipleObjects rather then WaitForSingleObject
in a loop. Sure, you can often redesign things so that
you don't need multiple objects in the first place but
that's a separate issue.
David Schwartz
2005-03-16 05:07:57 UTC
Permalink
Post by Pavel Lebedinsky
Post by Joe Seigh
There are emulations of the windows WaitForMultipleObjects out
there, but they're pretty inefficient.
So is the windows implementation but that's hidden from you.
Inefficient compared to what?
Inefficient compares to not waiting for multiple objects.
Post by Pavel Lebedinsky
If you have a bunch of objects that you want to wait for
then I would expect it to be more efficient to use
WaitForMultipleObjects rather then WaitForSingleObject
in a loop. Sure, you can often redesign things so that
you don't need multiple objects in the first place but
that's a separate issue.
No, that's not a separate issue, that's his point. It's easy to say "Oh,
there's this WaitForMultipleObjects function, so there's no reason for me to
redesign so I don't need multiple objects". His point is that even though
it's one nice neat function, it's inefficient. Your best bet is to architect
so that you don't need it.

You may say, "Eww, if I code my own, it will be ugly and inefficient".
His point is that the version Windows gives you is ugly and inefficient, the
ugliness and inefficiency is just hidden from you.

DS
gg
2005-03-16 14:38:32 UTC
Permalink
You may say, "Eww, if I code my own, it will be ugly and inefficient".
His point is that the version Windows gives you is ugly and inefficient,
the ugliness and inefficiency is just hidden from you.
DS
you can also use some pipes and use select instead of wait and write on pipe
instead of signal.
vikky@gmail.com
2005-03-16 13:41:36 UTC
Permalink
Post by Joe Seigh
Post by ***@gmail.com
Hi ,
I am porting an application from windows to linux. It uses
function
Post by Joe Seigh
Post by ***@gmail.com
WaitForMultipleObjects() to wait on multiple events and on the basis of
what event is signalled (which is specified by the functions return
value) it takes furthur action. The function is actually being used
here to wait for only two events,in mode where it returns if any of the
event is signalled. The first one being any event while the second one
always being the event signalled when the application is closing.
I am using pthreads in linux to port the application but i got stuck on
how to implement WaitForMultipleObjects() semantics. The closest
analogue i found to WaitForSingleObject() is using condition
variables
Post by Joe Seigh
Post by ***@gmail.com
and waiting on them. But alas i can't find any function in pthread
library that can wait on multiple condition varibles. I want to ask how
a suitable replacement for WaitForMultipleObjects() can be made.
You have to use the same condition variable.
For bWaitAll == true
while (!(cond1 && cond2 && ...))
pthread_cond_wait(&cvar, &mutex);
For bWaitAll == false
while (!(cond1 || cond2 || ...))
pthread_cond_wait(&cvar, &mutex);
Use broadcast instead of signal if you have multiple waiters.
There are emulations of the windows WaitForMultipleObjects out
there, but they're pretty inefficient. So is the windows
implementation but that's hidden from you.
--
Joe Seigh
Thanks for the answer Joe but there is one problem. You suggested that
i could use multiple conditions and use their ANDing/ORing. However how
do you suggest to represent the condition pertaining to closing of the
application?
David Schwartz
2005-03-16 16:16:31 UTC
Permalink
Post by ***@gmail.com
Thanks for the answer Joe but there is one problem. You suggested that
i could use multiple conditions and use their ANDing/ORing. However how
do you suggest to represent the condition pertaining to closing of the
application?
With a variable, 'shutdown', and a shutdown mutex. When you want to shut
the application down, lock the shutdown mutex, set 'shutdown' to 'true',
unlock the shutdown mutex, and broadcast the condition variable. Have
threads periodically lock the shutdown mutex, check the 'shutdown' variable,
and if it's 'true', terminate cleanly.

DS
Markus Elfring
2005-03-16 21:39:26 UTC
Permalink
Post by Joe Seigh
There are emulations of the windows WaitForMultipleObjects out
there, but they're pretty inefficient. So is the windows
implementation but that's hidden from you.
Which implementations have you got in mind besides this one?
"Porting of Win32 API WaitFor to Solaris Platform"
http://developers.sun.com/solaris/articles/waitfor_api.html

Do you know more benchmarks and speed comparisons like this one?
"Compound Win32 Synchronization Objects" by Ruediger R. Asche
http://msdn.microsoft.com/library/en-us/dndllpro/html/msdn_locktest.asp

Regards,
Markus
Markus Elfring
2005-03-17 21:39:27 UTC
Permalink
Post by Markus Elfring
Which implementations have you got in mind besides this one?
How do you think about this approach for the function "WaitForMultipleObjects"?
Does the code show interesting design alternatives?
http://source.winehq.org/source/dlls/kernel/sync.c#L128
http://cvs.winehq.org/cvsweb/wine/dlls/kernel/sync.c

Regards,
Markus

Continue reading on narkive:
Loading...