From d3e8a2361f78ef505c8cae2e30faefbfae5c88e1 Mon Sep 17 00:00:00 2001 From: steven-na Date: Fri, 14 Aug 2026 13:05:46 -0700 Subject: Fix race condition on thread intialization --- src/threadpool.c | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) (limited to 'src/threadpool.c') diff --git a/src/threadpool.c b/src/threadpool.c index a860cf7..68b34d6 100644 --- a/src/threadpool.c +++ b/src/threadpool.c @@ -1,11 +1,9 @@ #include "common.h" #include "threadpool.h" -#include "log.h" #include "smrt_arena.h" #include "deque.h" #include -#include #include #include #include @@ -65,14 +63,8 @@ thread_pool_t *tp_create(smrt_arena_t *arena, u64 max_jobs, u64 num_threads) { ts_deque_t tsq = ts_deque_create(arena, q); pthread_t *threads = SMRTA_ALLOC_ARRAY(arena, pthread_t, num_threads); + // I just leak this into the arena worker_args * args = SMRTA_ALLOC_ARRAY(arena, worker_args, num_threads); - for (u64 i = 0; i < num_threads; i++) { - args[i] = (worker_args){ - .id = i, - .tp = tp, - }; - pthread_create(&threads[i], NULL, worker_proc, (void*)&args[i]); - } pthread_mutex_t *count_mtx = smrt_arena_push(arena, sizeof(pthread_cond_t), true); pthread_mutex_init(count_mtx, NULL); @@ -92,6 +84,16 @@ thread_pool_t *tp_create(smrt_arena_t *arena, u64 max_jobs, u64 num_threads) { .num_active=0, }; + for (u64 i = 0; i < num_threads; i++) { + args[i] = (worker_args){ + .id = i, + .tp = tp, + }; + pthread_create(&threads[i], NULL, worker_proc, (void*)&args[i]); + } + + while (tp->num_alive != num_threads) {} + return tp; } -- cgit v1.2.3