From 274c2d6160d6d7df86f87dc3b4167d8a79912f04 Mon Sep 17 00:00:00 2001 From: Mylloon Date: Sat, 20 Apr 2024 20:20:54 +0200 Subject: [PATCH] random task --- src/sched.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/sched.c b/src/sched.c index 6f4a0e7..35ceee9 100644 --- a/src/sched.c +++ b/src/sched.c @@ -71,6 +71,9 @@ sched_init(int nthreads, int qlen, taskfunc f, void *closure) return -1; } + // Initialise l'aléatoire + srand(time(NULL)); + pthread_t threads[nthreads]; for(int i = 0; i < nthreads; ++i) { if(pthread_create(&threads[i], NULL, sched_worker, &sched) != 0) { @@ -155,7 +158,13 @@ sched_worker(void *arg) continue; } - // Extrait la tâche de la pile + // Extrait une tâche aléatoire de la liste + int random_index = rand() % (s->top + 1); + + struct task_info echange = s->tasks[random_index]; + s->tasks[random_index] = s->tasks[s->top]; + s->tasks[s->top] = echange; + taskfunc f = s->tasks[s->top].f; void *closure = s->tasks[s->top].closure; s->top--;