1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 |
/***************************************** ThreadPool.h Copyright (c) 2017 David Feng Distributed under the MIT License. *****************************************/ #ifndef THREAD_POOL_H #define THREAD_POOL_H #include <boost/thread/thread.hpp> #include <boost/thread/condition.hpp> #include <boost/thread/mutex.hpp> #include <iostream> #include <string> #include <map> #include <boost/function.hpp> #include <deque> #include <boost/shared_ptr.hpp> /* Design a ThreadPool class that manages a fixed given number of standby threads that are ready to perform concurrent task execution. Tasks are in the form of a function whose signature is to return a string and take an arbitrary number of arguments. The method that ThreadPool dispatches tasks should return immediately without a waiting time (asynchronous-callback). for infinite loop: std::thread's dtor will cause error without calling detach before dtor. If detach is called before dtor, dtor will work fine, but the infinite loop will continue to execute after dtor is called. boost::thread's dtor won't cause error without calling detach before dtor. The infinite loop will continue to execute after dtor is called. boost::thread's interrupt works only for interrupt points. Key ideas: 1. In ThreadPool, maintain a m_dispatchHandler thread to dispatch queued tasks to queued available Worker threads, and delete all Worker objects when ThreadPool's dtor is called. In this way, ThreadPool::Dispatch can return immediately (asynchronous callback). 2. ThreadPool::Dispatch enqueues a binded function object with 0 argument(note that arbitrary arguments can be binded into the function object), unblock m_dispatchHandler, and returns a shared pointer to a Future object 3. In ThreadPool dtor, mark m_destroyed, unblock m_dispatchHandler thread to delete all Worker objects, and wait for m_dispatchHandler to finish deletion and join. 4. In Worker, maintain a m_worker thread that invokes Worker::Invoke. In Worker::Invoke, keep scoped shared points to Worker's data member, keep a infinite while loop to execute its new task, and push back the m_standBy queue in ThreadPool when a task is done iff the Worker dtor critical section hasn't been entered. 5. Worker::Accept updates m_f and m_future, and unblocks m_worker thread to execute m_f. 6. In Worker dtor, interrupt and detach the m_worker thread. Since m_mutex_f, m_condition_f, m_f, m_mutex, m_destroyed are all in shared pointers, and m_worker thread is detached, the Worker object can be safely destroyed while m_worker thread is still running. */ namespace thread_pool { struct Future { boost::mutex mutex; std::string result; }; class Worker; class ThreadPool { friend Worker; public: explicit ThreadPool(size_t count); ~ThreadPool(); boost::shared_ptr<Future> Dispatch(boost::function0<std::string> f);//any function(whose return type should be string) with arbitrary arguments can be binded to a function object with 0 arguments private: void DispatchHandler(); boost::mutex m_mutex; boost::condition m_condition; std::map<boost::thread::id, Worker *> m_pool; std::deque<boost::thread::id> m_standBy; std::deque<std::pair<boost::function0<std::string>, boost::shared_ptr<Future> > > m_tasks; bool m_destroyed; boost::thread m_dispatchHandler; }; class Worker { public: explicit Worker(ThreadPool * t); ~Worker(); boost::thread::id GetId() const; void Accept(boost::function0<std::string> f, boost::shared_ptr<Future> future); private: void Invoke(); ThreadPool * m_ThreadPool; boost::shared_ptr<boost::mutex> m_mutex_f; boost::shared_ptr<boost::condition> m_condition_f; boost::shared_ptr<boost::function0<std::string> > m_f; boost::shared_ptr<Future> m_future; boost::shared_ptr<boost::mutex> m_mutex; boost::shared_ptr<bool> m_destroyed; boost::thread m_worker; }; Worker::Worker(ThreadPool * t) : m_ThreadPool(t) , m_mutex_f(new boost::mutex()), m_condition_f(new boost::condition()), m_f(), m_future() , m_mutex(new boost::mutex()), m_destroyed(new bool(false)) , m_worker(std::bind(&Worker::Invoke, this)) { } Worker::~Worker() { /*critical section: main*/ boost::mutex::scoped_lock lock(*m_mutex); *m_destroyed = true; m_worker.interrupt(); m_worker.detach(); } boost::thread::id Worker::GetId() const { return m_worker.get_id(); } void Worker::Accept(boost::function0<std::string> f, boost::shared_ptr<Future> future) { /*critical section: f*/ boost::mutex::scoped_lock lock(*m_mutex_f); boost::shared_ptr<boost::function0<std::string>>(new boost::function0<std::string>(f)).swap(m_f); m_future.swap(future); m_condition_f->notify_one(); } void Worker::Invoke() { boost::shared_ptr<boost::mutex> scoped_m_mutex_f = m_mutex_f; boost::shared_ptr<boost::condition> scoped_m_condition_f = m_condition_f; boost::shared_ptr<boost::mutex> scoped_m_mutex = m_mutex; boost::shared_ptr<bool> scoped_m_destroyed = m_destroyed; while (true) { { /*critical section: f*/ boost::mutex::scoped_lock lock(*scoped_m_mutex_f); scoped_m_condition_f->wait(lock); boost::shared_ptr<boost::function0<std::string> > scoped_m_f = m_f; boost::shared_ptr<Future> scoped_m_future = m_future; try { boost::mutex::scoped_lock lock(scoped_m_future->mutex); scoped_m_future->result = (*scoped_m_f)(); } catch (boost::thread_interrupted &) { } catch (std::exception &) { } catch (...) { } } /*critical section: main*/ boost::mutex::scoped_lock lock(*scoped_m_mutex); if (!(*scoped_m_destroyed)) { boost::mutex::scoped_lock lock2(m_ThreadPool->m_mutex);//nested lock: potential deadlock !! m_ThreadPool->m_condition.notify_one(); m_ThreadPool->m_standBy.push_back(boost::this_thread::get_id()); } else break; } } ThreadPool::ThreadPool(size_t count) : m_mutex(), m_condition(), m_pool(), m_standBy(), m_tasks(), m_destroyed(false) , m_dispatchHandler(std::bind(&ThreadPool::DispatchHandler, this)) { for (size_t i = 0; i < count; ++i) { Worker * w = new Worker(this); m_pool.emplace(w->GetId(), w); m_standBy.push_back(w->GetId()); } } ThreadPool::~ThreadPool() { { boost::mutex::scoped_lock lock(m_mutex); m_destroyed = true; m_condition.notify_one(); } m_dispatchHandler.join();//wait until all Workers have been destroyed } boost::shared_ptr<Future> ThreadPool::Dispatch(boost::function0<std::string> f) { boost::mutex::scoped_lock lock(m_mutex); boost::shared_ptr<Future> future(new Future); m_tasks.push_back(std::make_pair(f, future)); m_condition.notify_one(); return future; } void ThreadPool::DispatchHandler() { boost::mutex::scoped_lock lock(m_mutex); while (true) { m_condition.wait(lock); if (m_destroyed) { lock.unlock();//unlock m_mutex before invoking Worker::dtor to avoid deadlock for (const auto & p : m_pool) delete p.second; m_pool.clear(); break; } while (!m_standBy.empty() && !m_tasks.empty()) { m_pool[m_standBy.front()]->Accept(m_tasks.front().first, m_tasks.front().second); m_standBy.pop_front(); m_tasks.pop_front(); } } } /*the following functions are for test*/ std::string aLoop(std::string arg, size_t count) { for (size_t i = 0; i < count; ++i) std::cout << arg; std::ostringstream oss; oss << "[" << arg << "," << boost::this_thread::get_id() << "," << count << "]"; return oss.str(); } void Test() { std::cout << "Hit ENTER -> ThreadPool: init for 5 threads in pool. "; std::cin.ignore(); ThreadPool p(5); std::cout << "Hit ENTER -> ThreadPool: dispatch 200 tasks with random number of loops in [1:150]. "; std::cin.ignore(); std::vector<boost::shared_ptr<Future> > res; for (size_t i = 0; i < 200; ++i) res.push_back(p.Dispatch(std::bind(aLoop, std::to_string(i), rand() % 150 + 1))); std::cout << "ThreadPool: print 200 tasks' results [task_id,thread_id,loop_count]. "; std::cin.ignore(); std::ostringstream oss; for (const auto & p : res) oss << (*p).result << ","; std::cout << oss.str() << std::endl; std::cout << "Hit ENTER -> ThreadPool: detach all threads in pool. "; std::cin.ignore(); } } #endif |
Copyright © 2016-2020 Qualgame, LLC