Rafi Cohen wrote:
Hi, I'm asking for further assistance for yet another problem I
encounter with my project, this time concerning multithreads.
In order to explain my problem, I'll write a short example:
main:
pthread_mutex_lock(&mut);
flag = 0;
pthread_cond_broadcast(&cond);
printf("after signal: flag=%d\n", flag);
pthread_mutex_unlock(&mut);
........
thread:
pthread_mutex_lock(&mut);
while (flag)
{
pthread_cond_wait(&cond, &mut);
printf("after wait: flag=%d\n", flag);
}
pthread_mutex_unlock(&mut);
this code looks ok.
Now, after signal I indeed see that flag is 0.
Flag is assigned 1 in 2 other places in main, in both cases surrounded
by lock and unlock of the same mutex.
this sounds bad.
Wha happens, that after wait, flag is still 1 and the thread is stuck in
the loop, and I feel helpless.
when the main thread has set flag to 0, sent a broadcast and unlocked
the mutex, you are not guranteed which of the threads waiting on this
mutex will wake up first and acquire the mutex.
it could be that main's code continues running, locks the mutex again
and set flag to 1.
all this shows you've got a bad design. for instance - why does 'main'
set the flag to 1? this is racy. what is te purpose of this flag? what
are you trying to accomplish?
--guy
=================================================================
To unsubscribe, send mail to [EMAIL PROTECTED] with
the word "unsubscribe" in the message body, e.g., run the command
echo unsubscribe | mail [EMAIL PROTECTED]