Chris Quenelle's Weblog
Thoughts on developer tools.

All | Dbx | Development Tools | Life in General | OpenSolaris | plus | Software Philosophy | Sun Studio

fav comics

« Previous day (Sep 14, 2006) | Main | Next day (Sep 16, 2006) »
20060915 Friday September 15, 2006

Data Race in my last example

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

Older blog entries:

mug shot Chris Quenelle is a tools developer at Sun Microsystems. He's worked on performance and debugging tools at Sun for more than 10 years. He reads comic books and science fiction, and has more tivos than he can keep track of.

Calendar

RSS Feeds

Search

Links

Navigation

Referers