Friday September 15, 2006

I got some polite email from Yuan Lin pointing out that my example program had a data race in it. He suggested that I should change my food_on_table function as follows to avoid the race.
From
int food_on_table() {
pthread_mutex_lock(&foodlock);
if (food>0) {
food--;
}
pthread_mutex_unlock(&foodlock);
return food;
}
to
int food_on_table() {
int current_food;
pthread_mutex_lock(&foodlock);
current_food = food;
if (food>0) {
food--;
current_food = food;
}
pthread_mutex_unlock(&foodlock);
return current_food;
}
Good catch, Yuan! Thanks for the tip. Next time I'll use our Data Race Detection Tool.
Posted by Chris Quenelle
( Sep 15 2006, 04:23:16 PM PDT )
-
Permalink
-
-
Development Tools
Older blog entries: