From de09fb7dff67be7c1a58e5be9fee4b4a9ca3a265 Mon Sep 17 00:00:00 2001 From: Heng Li Date: Sat, 4 Mar 2017 09:03:57 -0500 Subject: [PATCH] use long for forpool --- kthread.c | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/kthread.c b/kthread.c index d83df88..986f695 100644 --- a/kthread.c +++ b/kthread.c @@ -158,13 +158,15 @@ void kt_forpool_destroy(void *_fp) void kt_forpool(void *_fp, void (*func)(void*,long,int), void *data, long n) { kt_forpool_t *fp = (kt_forpool_t*)_fp; - int i; - fp->n = n, fp->func = func, fp->data = data, fp->n_pending = fp->n_threads; - for (i = 0; i < fp->n_threads; ++i) fp->w[i].i = i, fp->w[i].action = 1; - pthread_mutex_lock(&fp->mutex); - pthread_cond_broadcast(&fp->cv_s); - while (fp->n_pending) pthread_cond_wait(&fp->cv_m, &fp->mutex); - pthread_mutex_unlock(&fp->mutex); + long i; + if (fp && fp->n_threads > 1) { + fp->n = n, fp->func = func, fp->data = data, fp->n_pending = fp->n_threads; + for (i = 0; i < fp->n_threads; ++i) fp->w[i].i = i, fp->w[i].action = 1; + pthread_mutex_lock(&fp->mutex); + pthread_cond_broadcast(&fp->cv_s); + while (fp->n_pending) pthread_cond_wait(&fp->cv_m, &fp->mutex); + pthread_mutex_unlock(&fp->mutex); + } else for (i = 0; i < n; ++i) func(data, i, 0); } /*****************