multithread problem

2007-05-21 Thread Rafi Cohen
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);
..
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.
Wha happens, that after wait, flag is still 1 and the thread is stuck in
the loop, and I feel helpless.
This code is based on my understandings from the documentation about
threads and also follows the examples I saw.
What should I do so that after wait flag will be 0 as I expect it to be
according to this example?
If this is not directly in this code piece, maybe somebody can give me
some clues what may cause flag to remain 1 after wait to see if anything
of this exists in the whole program.
Thanks, Rafi.


Re: multithread problem

2007-05-21 Thread guy keren

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]



RE: multithread problem

2007-05-22 Thread Rafi Cohen
Hi Guy, thanks for your reply. I should have debugged this better,
indeed I found the racy condition.
I should happen in a most rare and extreme case, but since it did, it
required it's treatment and solution.
Now problem is solved and everything works appropriately.
Thanks, Rafi.

-Original Message-
From: guy keren [mailto:[EMAIL PROTECTED] 
Sent: Monday, May 21, 2007 1:49 PM
To: Rafi Cohen
Cc: [EMAIL PROTECTED] Org. Il
Subject: Re: multithread problem


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



-- 
No virus found in this incoming message.
Checked by AVG Free Edition. 
Version: 7.5.467 / Virus Database: 269.7.6/813 - Release Date: 5/20/2007
7:54 AM



=
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]