This is the mail archive of the
pthreads-win32@sources.redhat.com
mailing list for the pthreas-win32 project.
Re: waiting for more than one cond in pThread question
On Wed, 03 Apr 2002 10:15:03 +1000, Ross Johnson
<rpj@ise.canberra.edu.au> wrote:
>Keith Willis wrote:
>>
>> OK, how about you associate one mutex/condvar with the _set_ of
>> predicate conditions[1], and do something like:
>>
>> ============================================================
>> pthread_mutex_lock(&mutex);
>> while (predicate_one == FALSE && predicate_two == FALSE)
>> {
>> pthread_cond_wait(&condvar, &mutex);
>> }
>> pthread_unlock_mutex(&mutex);
>>
>> if (predicate_one == TRUE)
>> do_something();
>> else if (predicate_two == TRUE)
>> do_something_else();
>> else
>> something_horrible_happened();
>> ============================================================
>>
>
>... except you need to unlock the mutex after you've finished
>using or copying the predicate values locally, otherwise
>you've got a race condition.
So, something more like this might do the trick?
============================================================
pthread_mutex_lock(&mutex);
while (predicate_one == FALSE && predicate_two == FALSE)
{
pthread_cond_wait(&condvar, &mutex);
}
if (predicate_one == TRUE)
{
pthread_unlock_mutex(&mutex);
do_something();
}
else if (predicate_two == TRUE)
{
pthread_unlock_mutex(&mutex);
do_something_else();
}
else
{
pthread_unlock_mutex(&mutex);
something_horrible_happened();
}
============================================================
--
PGP key ID 0xEB7180EC
Available from http://www.keyserver.net