aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDimitri Sokolyuk <demon@dim13.org>2009-10-04 01:34:17 +0000
committerDimitri Sokolyuk <demon@dim13.org>2009-10-04 01:34:17 +0000
commitcc21d0e2206b706dd0fa7eed2ab425623db1081e (patch)
tree642be3fbe6e71c84a7f41d353a19e3cc931cc393
parentd693267381bb182d17e38f690100ee7118716faf (diff)
fix mutex, dynamic threads
-rw-r--r--weasel.c52
1 files changed, 35 insertions, 17 deletions
diff --git a/weasel.c b/weasel.c
index 1b6b765..8712a5e 100644
--- a/weasel.c
+++ b/weasel.c
@@ -25,8 +25,7 @@
#include <time.h>
#include <unistd.h>
-#define NUM_THREADS 10
-pthread_t threads[NUM_THREADS];
+pthread_t *threads;
pthread_mutex_t mutexsum;
pthread_attr_t attr;
@@ -116,17 +115,24 @@ printpopulation(int maximal)
struct creature *
pickrandom(int population)
{
- int n = random() % population;
+ int n;
- cp = SLIST_FIRST(&generation);
- while (n-- > 0 && cp)
- cp = SLIST_NEXT(cp, link);
- if (cp->locked)
- cp = pickrandom(population);
+ for (;;) {
+ n = random() % population;
+ SLIST_FOREACH(cp, &generation, link)
+ if (n-- <= 0)
+ break;
- pthread_mutex_lock(&mutexsum);
- cp->locked = 1;
- pthread_mutex_unlock(&mutexsum);
+ if (cp->locked)
+ continue;
+
+ pthread_mutex_lock(&mutexsum);
+ assert(!cp->locked);
+ cp->locked = 1;
+ pthread_mutex_unlock(&mutexsum);
+
+ break;
+ }
return cp;
}
@@ -207,15 +213,25 @@ main(int argc, char **argv)
extern int LINES;
int population = 1000;
int mutationrate = 100; /* 1/n */
+ int nthreads = population / 10;
int i, c, t;
- while ((c = getopt(argc, argv, "m:p:")) != -1)
+ while ((c = getopt(argc, argv, "m:p:t:")) != -1)
switch (c) {
case 'm':
- mutationrate = atoi(optarg);
+ i = atoi(optarg);
+ if (i > 0)
+ mutationrate = i;
break;
case 'p':
- population = atoi(optarg);
+ i = atoi(optarg);
+ if (i > 0)
+ population = i;
+ break;
+ case 't':
+ i = atoi(optarg);
+ if (i > 0)
+ nthreads = i;
break;
default:
break;
@@ -232,14 +248,15 @@ main(int argc, char **argv)
args.population = population;
args.mutationrate = mutationrate;
+ threads = calloc(nthreads, sizeof(pthread_t));
+ assert(threads);
pthread_mutex_init(&mutexsum, NULL);
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
-
for (i = 0;; i++) {
move(0, 0);
- for (t = 0; t < NUM_THREADS; t++)
+ for (t = 0; t < nthreads; t++)
pthread_create(&threads[t], &attr, ptintercourse, (void *)&args);
printpopulation(LINES - 8);
if (success())
@@ -249,8 +266,9 @@ main(int argc, char **argv)
pthread_attr_destroy(&attr);
pthread_mutex_destroy(&mutexsum);
+ free(threads);
- printw("\nhalted after %d generations\n", NUM_THREADS * i / population);
+ printw("\nhalted after %d generations\n", nthreads * i / population);
refresh();
endwin();