add threadpool_join

Sat, 06 Dec 2025 14:12:11 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sat, 06 Dec 2025 14:12:11 +0100
changeset 949
ef8f13c8c08f
parent 948
94bc57d63128
child 950
39641cf150eb

add threadpool_join

ui/common/threadpool.c file | annotate | diff | comparison | revisions
ui/common/threadpool.h file | annotate | diff | comparison | revisions
--- a/ui/common/threadpool.c	Thu Dec 04 19:04:08 2025 +0100
+++ b/ui/common/threadpool.c	Sat Dec 06 14:12:11 2025 +0100
@@ -54,10 +54,11 @@
 }
 
 int threadpool_start(UiThreadpool *pool) {
+    pool->nthreads = pool->min_threads;
+    pool->threads = calloc(pool->max_threads, sizeof(pthread_t));
     /* create pool threads */
-    for(int i=0;i<pool->min_threads;i++) {
-        pthread_t t;
-        if (pthread_create(&t, NULL, threadpool_func, pool) != 0) {
+    for(int i=0;i<pool->nthreads;i++) {
+        if (pthread_create(&pool->threads[i], NULL, threadpool_func, pool) != 0) {
             fprintf(stderr, "uic: threadpool_start: pthread_create failed: %s", strerror(errno));
             return 1;
         }
@@ -65,6 +66,16 @@
     return 0;
 }
 
+int threadpool_join(UiThreadpool *pool) {
+    int err = 0;
+    for(int i=0;i<pool->nthreads;i++) {
+        if(pthread_join(pool->threads[i], NULL)) {
+            err = 1;
+        }
+    }
+    return err;
+}
+
 void* threadpool_func(void *data) {
     UiThreadpool *pool = (UiThreadpool*)data;
     
--- a/ui/common/threadpool.h	Thu Dec 04 19:04:08 2025 +0100
+++ b/ui/common/threadpool.h	Sat Dec 06 14:12:11 2025 +0100
@@ -62,6 +62,8 @@
     uint32_t        num_idle;
     int             min_threads;
     int             max_threads;
+    pthread_t       *threads;
+    int             nthreads;
 };
 
 struct _threadpool_job {
@@ -76,6 +78,7 @@
 
 UiThreadpool* threadpool_new(int min, int max);
 int threadpool_start(UiThreadpool *pool);
+int threadpool_join(UiThreadpool *pool);
 void* threadpool_func(void *data);
 threadpool_job* threadpool_get_job(UiThreadpool *pool);
 void threadpool_run(UiThreadpool *pool, job_callback_f func, void *data);

mercurial