Working on multi-threading, including web
This commit is contained in:
1 parent
b055faf882
commit
648acf8938
12 files changed
+230
-45
No files matched your search
Whitespace-only changes.
@@ -0,0 +1,43 @@
|
||||
#pragma once
|
||||
#include <boost/asio.hpp>
|
||||
#include <functional>
|
||||
#include <vector>
|
||||
#include <thread>
|
||||
#include <memory>
|
||||
|
||||
namespace ZL {
|
||||
|
||||
class TaskManager {
|
||||
private:
|
||||
boost::asio::io_context ioContext;
|
||||
std::unique_ptr<boost::asio::executor_work_guard<boost::asio::io_context::executor_type>> workGuard;
|
||||
std::vector<std::thread> workers;
|
||||
|
||||
public:
|
||||
//TaskManager(size_t threadCount = std::thread::hardware_concurrency()) {
|
||||
TaskManager(size_t threadCount = 2) {
|
||||
workGuard = std::make_unique<boost::asio::executor_work_guard<boost::asio::io_context::executor_type>>(ioContext.get_executor());
|
||||
|
||||
for (size_t i = 0; i < threadCount; ++i) {
|
||||
workers.emplace_back([this]() {
|
||||
ioContext.run();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// Ìåòîä äëÿ äîáàâëåíèÿ ôîíîâîé çàäà÷è
|
||||
void EnqueueBackgroundTask(std::function<void()> task) {
|
||||
boost::asio::post(ioContext, task);
|
||||
}
|
||||
|
||||
// Graceful shutdown
|
||||
~TaskManager() {
|
||||
workGuard.reset(); // Ðàçðåøàåì ioContext.run() çàâåðøèòüñÿ, êîãäà çàäà÷ íå îñòàíåòñÿ
|
||||
ioContext.stop(); // Îïöèîíàëüíî: íåìåäëåííàÿ îñòàíîâêà
|
||||
for (auto& t : workers) {
|
||||
if (t.joinable()) t.join();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace ZL
|
||||
Reference in new issue
Block a user