From 833b16d109444ed15643246f1f419d6e96f101c3 Mon Sep 17 00:00:00 2001 From: Jan200101 Date: Fri, 2 Sep 2022 18:19:53 +0200 Subject: stop threads when condition is reached, gracefully deal with status text --- src/threading/pool.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) (limited to 'src/threading/pool.c') diff --git a/src/threading/pool.c b/src/threading/pool.c index 266254e..1598852 100644 --- a/src/threading/pool.c +++ b/src/threading/pool.c @@ -9,6 +9,7 @@ struct worker_t { struct pool_t* pool; int id; + int done; }; struct pool_t* pool_init() @@ -51,19 +52,17 @@ static void* task_executor(void* pinfo) { struct pool_task_t* pool_end = pool->tasks + pool->pool_size; struct pool_task_t* task = pool->task_next++; - while (pool_end > task) + while (pool_end > task && (pool->condition == NULL || *pool->condition)) { if (!task->done) { task->func(task->arg); task->done = 1; } - task = pool->task_next++; } } - - return NULL; + worker->done = 1; } void pool_complete(struct pool_t* pool) @@ -81,13 +80,15 @@ void pool_complete(struct pool_t* pool) worker->pool = pool; worker->id = i; + worker->done = 0; pthread_create(thread, NULL, task_executor, worker); } for (int i = 0; i < pool->workers; ++i) { - pthread_join(threads[i], NULL); + if (!workers[i].done) + pthread_join(threads[i], NULL); } free(threads); -- cgit v1.2.3