nuclear@24: #include "threadpool.h" nuclear@24: nuclear@24: ThreadPool::ThreadPool(int num_threads) nuclear@24: { nuclear@24: quit = false; nuclear@24: nuclear@24: if(num_threads == -1) { nuclear@24: num_threads = std::thread::hardware_concurrency(); nuclear@24: } nuclear@24: nuclear@24: printf("creating thread pool with %d threads\n", num_threads); nuclear@24: nuclear@24: thread = new std::thread[num_threads]; nuclear@24: for(int i=0; i func) nuclear@24: { nuclear@24: std::unique_lock lock(workq_mutex); nuclear@24: workq.push_back(func); nuclear@24: } nuclear@24: nuclear@24: void ThreadPool::thread_func() nuclear@24: { nuclear@24: std::unique_lock lock(workq_mutex); nuclear@24: for(;;) { nuclear@24: if(quit) break; nuclear@24: nuclear@24: condvar.wait(lock); nuclear@24: nuclear@24: if(!quit && !workq.empty()) { nuclear@24: std::function work = workq.front(); nuclear@24: workq.pop_front(); nuclear@24: lock.unlock(); nuclear@24: nuclear@24: work(); nuclear@24: nuclear@24: lock.lock(); nuclear@24: } nuclear@24: } nuclear@24: }