From d7cb24d23f798deda728610caf567f343136ee3e Mon Sep 17 00:00:00 2001 From: Kubat <mael.martin31@gmail.com> Date: Sat, 26 Oct 2019 16:41:18 +0200 Subject: [PATCH] the pool is working as axpected, but if it's asked to do more jobs than the number of threads available new jobs won't be treated. --- player/main.c | 13 +++++++++++++ player/thread_pool.c | 11 ++++++++++- 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/player/main.c b/player/main.c index 3fa1edaf..7fec4ce1 100644 --- a/player/main.c +++ b/player/main.c @@ -24,7 +24,20 @@ int main(int argc, char * argv[]) mpv_destruct(); #endif thread_pool_construct(10); + thread_pool_queue(func1, NULL); + thread_pool_queue(func1, NULL); + thread_pool_queue(func1, NULL); + thread_pool_queue(func1, NULL); + thread_pool_queue(func1, NULL); + thread_pool_queue(func1, NULL); + thread_pool_queue(func1, NULL); sleep(1); + thread_pool_queue(func1, NULL); + thread_pool_queue(func1, NULL); + thread_pool_queue(func1, NULL); + thread_pool_queue(func1, NULL); + thread_pool_queue(func1, NULL); + sleep(4); thread_pool_destruct(); return 0; } diff --git a/player/thread_pool.c b/player/thread_pool.c index 1246108a..c9a0bd80 100644 --- a/player/thread_pool.c +++ b/player/thread_pool.c @@ -43,11 +43,20 @@ void * worker_function(void * arg) { pthread_mutex_lock(¤t->mutex); - if (current->state == killed) + switch (current->state) { + case killed: pthread_mutex_unlock(¤t->mutex); pthread_mutex_destroy(¤t->mutex); pthread_exit(NULL); + break; + + case working: + current->function(current->arg); + current->state = waiting; + + default: + break; } pthread_mutex_unlock(¤t->mutex); -- GitLab