5 #include "lang/verify.h"
10 ThrPool *tp = (ThrPool *)arg;
21 //if blocking, then addJob() blocks when queue is full
22 //otherwise, addJob() simply returns false when queue is full
23 ThrPool::ThrPool(int sz, bool blocking)
24 : nthreads_(sz),blockadd_(blocking),jobq_(100*sz)
26 pthread_attr_init(&attr_);
27 pthread_attr_setstacksize(&attr_, 128<<10);
29 for (int i = 0; i < sz; i++) {
31 VERIFY(pthread_create(&t, &attr_, do_worker, (void *)this) ==0);
36 //IMPORTANT: this function can be called only when no external thread
37 //will ever use this thread pool again or is currently blocking on it
40 for (int i = 0; i < nthreads_; i++) {
42 j.f = (void *(*)(void *))NULL; //poison pill to tell worker threads to exit
46 for (int i = 0; i < nthreads_; i++) {
47 VERIFY(pthread_join(th_[i], NULL)==0);
50 VERIFY(pthread_attr_destroy(&attr_)==0);
54 ThrPool::addJob(void *(*f)(void *), void *a)
60 return jobq_.enq(j,blockadd_);
64 ThrPool::takeJob(job_t *j)