diff --git a/services/lib/src/ThreadPool.h b/services/lib/src/ThreadPool.h new file mode 100644 --- /dev/null +++ b/services/lib/src/ThreadPool.h @@ -0,0 +1,47 @@ +#pragma once + +#include "GlobalTools.h" + +#include +#include + +#include +#include + +typedef std::function Task; +typedef std::function)> Callback; + +namespace comm { +namespace network { + +class ThreadPool { + boost::asio::thread_pool pool = + boost::asio::thread_pool(tools::getNumberOfCores()); + + ThreadPool() { + } + + virtual ~ThreadPool() { + } + +public: + static ThreadPool &getInstance() { + static ThreadPool instance; + return instance; + } + + void scheduleWithCallback(Task task, Callback callback) { + boost::asio::post(this->pool, [task, callback]() { + std::unique_ptr err = nullptr; + try { + task(); + } catch (std::exception &e) { + err = std::make_unique(e.what()); + } + callback(std::move(err)); + }); + } +}; + +} // namespace network +} // namespace comm