int threadpool_schedule(threadpool_t *threadpool, threadpool_func_t *func, void *closure) { threadpool_item_t *item; int rc = 0; int dosignal = 1; item = threadpool_item_alloc(func, closure); if(item == NULL) return -1; pthread_mutex_lock(&threadpool->lock); threadpool_enqueue(&threadpool->scheduled, item); if(threadpool->idle == 0) { dosignal = 0; if(threadpool->threads < threadpool->maxthreads) { rc = threadpool_new_thread(threadpool); if(rc < 0 && threadpool->threads > 0) rc = 0; /* we'll recover */ } } if(dosignal) pthread_cond_signal(&threadpool->cond); pthread_mutex_unlock(&threadpool->lock); return rc; }
int main(int argc, char *argv[]) { if ( argc != 3 ) { printf("usage: %s numtasks poolsize\n", argv[0]); exit(0); } int NUMTASKS = atoi(argv[1]); //1600 // 16000 int POOLSIZE = atoi(argv[2]); // 300 //868 struct timespec tspec; long long withpool, nopool, before; static long long waittime = 500; clock_gettime(CLOCK_REALTIME, &tspec); before = timespecToMs(tspec); struct threadpool * pool = threadpool_create(POOLSIZE); for (int i=0; i < NUMTASKS; ++i) { threadpool_enqueue(pool, busywait, &waittime); } threadpool_destroy(pool); clock_gettime(CLOCK_REALTIME, &tspec); withpool = timespecToMs(tspec) - before; clock_gettime(CLOCK_REALTIME, &tspec); before = timespecToMs(tspec); pthread_t threads[NUMTASKS]; for (int i=0; i < NUMTASKS; ++i) { pthread_create(&threads[i], NULL, busypthread, &waittime); } for(int i=0; i < NUMTASKS; ++i) { if(pthread_join(threads[i], NULL) != 0) { exit(EXIT_FAILURE); } } clock_gettime(CLOCK_REALTIME, &tspec); nopool = timespecToMs(tspec) - before; printf("Pool runtime: %lld\n",withpool); printf("Naked runtime: %lld\n",nopool); return 0; }
int threadpool_schedule_back(threadpool_t *threadpool, threadpool_func_t *func, void *closure) { threadpool_item_t *item; int wake = 1; item = threadpool_item_alloc(func, closure); if(item == NULL) return -1; pthread_mutex_lock(&threadpool->lock); if(threadpool->have_scheduled_back) wake = 0; /* Order is important. */ atomic_set(&threadpool->have_scheduled_back); threadpool_enqueue(&threadpool->scheduled_back, item); pthread_mutex_unlock(&threadpool->lock); if(wake && threadpool->wakeup) threadpool->wakeup(threadpool->wakeup_closure); return 0; }