Page MenuHomePhabricator

D5068.id16790.diff
No OneTemporary

D5068.id16790.diff

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 <boost/asio.hpp>
+#include <boost/asio/thread_pool.hpp>
+
+#include <atomic>
+#include <memory>
+
+typedef std::function<void()> Task;
+typedef std::function<void(std::unique_ptr<std::string>)> 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<std::string> err = nullptr;
+ try {
+ task();
+ } catch (std::exception &e) {
+ err = std::make_unique<std::string>(e.what());
+ }
+ callback(std::move(err));
+ });
+ }
+};
+
+} // namespace network
+} // namespace comm

File Metadata

Mime Type
text/plain
Expires
Sat, Nov 23, 7:13 AM (18 h, 3 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2569487
Default Alt Text
D5068.id16790.diff (1 KB)

Event Timeline