Skip to content

Commit 90240c2

Browse files
committed
threadpool: fix: min thread amount was ignored on POSIX
1 parent 18d4beb commit 90240c2

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

threadpool_pthread.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,18 +162,19 @@ static void *threadpool_do_work(void *arg) {
162162
pthread_exit(NULL);
163163
}
164164

165-
static void threadpool_create_thread_on_demand(threadpool_s *threadpool) {
165+
static bool threadpool_create_thread_on_demand(threadpool_s *threadpool) {
166166
// Create new thread and add it to the list of threads
167167
pthread_t handle = 0;
168168
if (pthread_create(&handle, NULL, threadpool_do_work, threadpool))
169-
return;
169+
return false;
170170

171171
threadpool_thread_s *thread = (threadpool_thread_s *)calloc(1, sizeof(threadpool_thread_s));
172172
thread->handle = handle;
173173
thread->next = threadpool->threads;
174174

175175
threadpool->threads = thread;
176176
threadpool->num_threads++;
177+
return true;
177178
}
178179

179180
bool threadpool_enqueue(void *ctx, void *user_data, threadpool_job_cb callback) {
@@ -189,6 +190,12 @@ bool threadpool_enqueue(void *ctx, void *user_data, threadpool_job_cb callback)
189190
// Add job to the job queue
190191
threadpool_enqueue_job(threadpool, job);
191192

193+
// Create min amount of threads
194+
while (threadpool->num_threads < threadpool->min_threads) {
195+
if (!threadpool_create_thread_on_demand(threadpool))
196+
break;
197+
}
198+
192199
// Create new thread if all threads are busy
193200
if (threadpool->busy_threads == threadpool->num_threads && threadpool->num_threads < threadpool->max_threads)
194201
threadpool_create_thread_on_demand(threadpool);

0 commit comments

Comments
 (0)