From f42777db6c22884341cb8b1bd673afdbb6943d0c Mon Sep 17 00:00:00 2001 From: Mylloon Date: Tue, 23 Apr 2024 10:55:03 +0200 Subject: [PATCH] fix thread never found, reducing performance --- src/sched.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/sched.c b/src/sched.c index 5d6a69d..e21bd1f 100644 --- a/src/sched.c +++ b/src/sched.c @@ -146,7 +146,10 @@ sched_init(int nthreads, int qlen, taskfunc f, void *closure) return sched_init_cleanup(-1); } + + pthread_mutex_lock(&sched.mutex); sched.nthreads++; + pthread_mutex_unlock(&sched.mutex); } for(int i = 0; i < nthreads; ++i) { @@ -200,11 +203,15 @@ int current_thread(struct scheduler *s) { pthread_t current = pthread_self(); + + pthread_mutex_lock(&s->mutex); for(int i = 0; i < s->nthreads; i++) { if(pthread_equal(s->threads[i], current)) { + pthread_mutex_unlock(&s->mutex); return i; } } + pthread_mutex_unlock(&s->mutex); return -1; }