erebus

annotate liberebus/src/threadpool.cc @ 26:c8a6fb04fefa

multithreadededit
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 01 Jun 2014 19:19:40 +0300
parents 6ef4b10fa468
children 53a98c148bf8
rev   line source
nuclear@26 1 #include <algorithm>
nuclear@26 2 #include <chrono>
nuclear@26 3 #include "threadpool.h"
nuclear@26 4
nuclear@26 5 using namespace std::chrono;
nuclear@26 6
nuclear@26 7 ThreadPool::ThreadPool(int num_threads)
nuclear@26 8 {
nuclear@26 9 quit = false;
nuclear@26 10 qsize = 0;
nuclear@26 11 nactive = 0;
nuclear@26 12
nuclear@26 13 if(num_threads == -1) {
nuclear@26 14 num_threads = std::thread::hardware_concurrency();
nuclear@26 15 }
nuclear@26 16
nuclear@26 17 printf("creating thread pool with %d threads\n", num_threads);
nuclear@26 18
nuclear@26 19 thread = new std::thread[num_threads];
nuclear@26 20 for(int i=0; i<num_threads; i++) {
nuclear@26 21 thread[i] = std::thread(&ThreadPool::thread_func, this);
nuclear@26 22 }
nuclear@26 23 this->num_threads = num_threads;
nuclear@26 24 }
nuclear@26 25
nuclear@26 26 ThreadPool::~ThreadPool()
nuclear@26 27 {
nuclear@26 28 quit = true;
nuclear@26 29 workq_condvar.notify_all();
nuclear@26 30
nuclear@26 31 printf("ThreadPool: waiting for %d worker threads to stop ", num_threads);
nuclear@26 32 fflush(stdout);
nuclear@26 33 for(int i=0; i<num_threads; i++) {
nuclear@26 34 thread[i].join();
nuclear@26 35 putchar('.');
nuclear@26 36 fflush(stdout);
nuclear@26 37 }
nuclear@26 38 putchar('\n');
nuclear@26 39 }
nuclear@26 40
nuclear@26 41 void ThreadPool::add_work(std::function<void ()> func)
nuclear@26 42 {
nuclear@26 43 add_work(func, std::function<void ()>{});
nuclear@26 44 }
nuclear@26 45
nuclear@26 46 void ThreadPool::add_work(std::function<void ()> work_func, std::function<void ()> done_func)
nuclear@26 47 {
nuclear@26 48 std::unique_lock<std::mutex> lock(workq_mutex);
nuclear@26 49 workq.push_back(WorkItem{work_func, done_func});
nuclear@26 50 ++qsize;
nuclear@26 51 workq_condvar.notify_all();
nuclear@26 52 }
nuclear@26 53
nuclear@26 54 int ThreadPool::queued() const
nuclear@26 55 {
nuclear@26 56 std::unique_lock<std::mutex> lock(workq_mutex);
nuclear@26 57 return qsize;
nuclear@26 58 }
nuclear@26 59
nuclear@26 60 int ThreadPool::active() const
nuclear@26 61 {
nuclear@26 62 std::unique_lock<std::mutex> lock(workq_mutex);
nuclear@26 63 return nactive;
nuclear@26 64 }
nuclear@26 65
nuclear@26 66 int ThreadPool::pending() const
nuclear@26 67 {
nuclear@26 68 std::unique_lock<std::mutex> lock(workq_mutex);
nuclear@26 69 return nactive + qsize;
nuclear@26 70 }
nuclear@26 71
nuclear@26 72 long ThreadPool::wait()
nuclear@26 73 {
nuclear@26 74 auto start_time = steady_clock::now();
nuclear@26 75
nuclear@26 76 std::unique_lock<std::mutex> lock(workq_mutex);
nuclear@26 77 done_condvar.wait(lock, [this](){ return nactive == 0 && workq.empty(); });
nuclear@26 78
nuclear@26 79 auto dur = steady_clock::now() - start_time;
nuclear@26 80 return duration_cast<milliseconds>(dur).count();
nuclear@26 81 }
nuclear@26 82
nuclear@26 83 long ThreadPool::wait(long timeout)
nuclear@26 84 {
nuclear@26 85 auto start_time = steady_clock::now();
nuclear@26 86 duration<long, std::milli> dur, timeout_dur(std::max(timeout, 5L));
nuclear@26 87
nuclear@26 88 std::unique_lock<std::mutex> lock(workq_mutex);
nuclear@26 89 while(timeout_dur.count() > 0 && (nactive > 0 || !workq.empty())) {
nuclear@26 90 if(done_condvar.wait_for(lock, timeout_dur) == std::cv_status::timeout) {
nuclear@26 91 break;
nuclear@26 92 }
nuclear@26 93 dur = duration_cast<milliseconds>(steady_clock::now() - start_time);
nuclear@26 94 timeout_dur = milliseconds(std::max(timeout, 5L)) - dur;
nuclear@26 95 }
nuclear@26 96
nuclear@26 97 /*printf("waited for: %ld ms (%ld req) (na %d,qs %d,em %s)\n", dur.count(), timeout,
nuclear@26 98 nactive, qsize, workq.empty() ? "true" : "false");*/
nuclear@26 99 return dur.count();
nuclear@26 100 }
nuclear@26 101
nuclear@26 102 void ThreadPool::thread_func()
nuclear@26 103 {
nuclear@26 104 std::unique_lock<std::mutex> lock(workq_mutex);
nuclear@26 105 for(;;) {
nuclear@26 106 if(quit) break;
nuclear@26 107
nuclear@26 108 workq_condvar.wait(lock);
nuclear@26 109
nuclear@26 110 while(!quit && !workq.empty()) {
nuclear@26 111 WorkItem witem = workq.front();
nuclear@26 112 workq.pop_front();
nuclear@26 113 ++nactive;
nuclear@26 114 --qsize;
nuclear@26 115 lock.unlock();
nuclear@26 116
nuclear@26 117 witem.work();
nuclear@26 118 if(witem.done) {
nuclear@26 119 witem.done();
nuclear@26 120 }
nuclear@26 121
nuclear@26 122 lock.lock();
nuclear@26 123 --nactive;
nuclear@26 124 done_condvar.notify_all();
nuclear@26 125 }
nuclear@26 126 }
nuclear@26 127 }
nuclear@26 128