Page Menu
Home
Phabricator
Search
Configure Global Search
Log In
Files
F3345770
D5068.id16586.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
1 KB
Referenced Files
None
Subscribers
None
D5068.id16586.diff
View Options
diff --git a/services/lib/src/Worker.h b/services/lib/src/Worker.h
new file mode 100644
--- /dev/null
+++ b/services/lib/src/Worker.h
@@ -0,0 +1,49 @@
+#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 Worker {
+ std::unique_ptr<boost::asio::thread_pool> pool;
+
+ Worker() {
+ this->pool =
+ std::make_unique<boost::asio::thread_pool>(tools::getNumberOfCpus());
+ }
+
+ virtual ~Worker() {
+ this->pool->join();
+ }
+
+public:
+ static Worker &getInstance() {
+ static Worker instance;
+ return instance;
+ }
+
+ void schedule(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
Details
Attached
Mime Type
text/plain
Expires
Sat, Nov 23, 6:56 AM (17 h, 21 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2569528
Default Alt Text
D5068.id16586.diff (1 KB)
Attached To
Mode
D5068: [services] Rust Integration - c++ - Add worker
Attached
Detach File
Event Timeline
Log In to Comment