From 9e5f7ef55ced908b24c0ac98b018006cb04cbd40 Mon Sep 17 00:00:00 2001 From: Dimitri Sokolyuk Date: Mon, 5 Oct 2009 14:26:59 +0000 Subject: fix race condition --- weasel.c | 18 +++++++----------- 1 file 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; -- cgit v1.2.3