1 /* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
6 #include "base/exception.hpp"
7 #include "base/logger.hpp"
13 #include <boost/asio/post.hpp>
14 #include <boost/asio/thread_pool.hpp>
15 #include <boost/thread/locks.hpp>
16 #include <boost/thread/shared_mutex.hpp>
35 typedef std::function<void ()> WorkFunction;
37 ThreadPool(size_t threads = std::thread::hardware_concurrency() * 2u);
44 * Appends a work item to the work queue. Work items will be processed in FIFO order.
46 * @param callback The callback function for the work item.
47 * @returns true if the item was queued, false otherwise.
50 bool Post(T callback, SchedulerPolicy)
52 boost::shared_lock<decltype(m_Mutex)> lock (m_Mutex);
55 boost::asio::post(*m_Pool, [callback]() {
58 } catch (const std::exception& ex) {
59 Log(LogCritical, "ThreadPool")
60 << "Exception thrown in event handler:\n"
61 << DiagnosticInformation(ex);
63 Log(LogCritical, "ThreadPool", "Exception of unknown type thrown in event handler.");
74 boost::shared_mutex m_Mutex;
75 std::unique_ptr<boost::asio::thread_pool> m_Pool;
81 #endif /* THREADPOOL_H */