36 #include <boost/asio.hpp>
37 #include <boost/thread.hpp>
51 boost::asio::io_service::work
work;
59 : impl(new
Impl(pool_size, queueTasks))
61 for (std::size_t i = 0; i < pool_size; ++i)
63 impl->threads.create_thread(boost::bind(&boost::asio::io_service::run,
71 impl->io_service.stop();
76 impl->threads.join_all();
78 catch (
const std::exception&) {}
83 std::unique_lock lock(impl->mutex);
86 if (!impl->queueTasks && impl->available == 0)
92 if (!impl->queueTasks)
96 auto promise = std::make_shared<std::promise<void>>();
98 impl->io_service.post([
this, task, promise]
101 promise->set_value();
103 return Handle {promise->get_future()};
108 std::unique_lock lock(impl->mutex);
109 if (!impl->queueTasks)
111 return impl->available;
119 void ThreadPool::wrap_task(std::function<
void ()> task)
127 catch (
const std::exception&)
129 std::unique_lock lock(impl->mutex);
131 if (!impl->queueTasks)
139 std::unique_lock lock(impl->mutex);
140 if (!impl->queueTasks)
148 mutex(new
std::mutex)
154 mutex(new
std::mutex),
155 functionFinished(functionFinished)
162 if (!joined && isValid() && !detached)
164 throw LocalException() <<
"You did not join the thread pool handle before the handle was deleted!";
170 return functionFinished.valid();
176 std::lock_guard<decltype(*mutex)> lock(*mutex);
184 throw LocalException() <<
"You cannot join a detached thread!";
186 functionFinished.get();
193 std::lock_guard<decltype(*mutex)> lock(*mutex);
201 throw LocalException() <<
"You cannot detach a joined thread!";
208 return functionFinished;