aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDimitri Sokolyuk <demon@dim13.org>2009-10-05 14:26:59 +0000
committerDimitri Sokolyuk <demon@dim13.org>2009-10-05 14:26:59 +0000
commit9e5f7ef55ced908b24c0ac98b018006cb04cbd40 (patch)
tree0e848bad1dd248be23ebbd454563f17d37669c91
parent19978bd155793d6cf67051ade1d83d6a101a1c9a (diff)
fix race condition
-rw-r--r--weasel.c18
1 files changed, 7 insertions, 11 deletions
diff --git a/weasel.c b/weasel.c
index 0358471..1c2e90e 100644
--- a/weasel.c
+++ b/weasel.c
@@ -114,20 +114,16 @@ printpopulation(int maximal)
struct creature *
pickrandom()
{
- int n;
-
- for (;;) {
- n = random() % population;
- cp = generation[n];
- if (cp->locked)
- continue;
+ int hasone = 0;
+ while (!hasone) {
+ cp = generation[random() % population];
pthread_mutex_lock(&mutexsum);
- assert(!cp->locked);
- cp->locked = 1;
+ if (!cp->locked) {
+ cp->locked = 1;
+ hasone = 1;
+ }
pthread_mutex_unlock(&mutexsum);
-
- break;
}
return cp;