Minor refactoring
This commit is contained in:
1 parent
4cc6971542
commit
dca38c986d
6 files changed
+93
-104
No files matched your search
@@ -0,0 +1,50 @@
|
||||
#include "TaskManager.h"
|
||||
|
||||
|
||||
namespace ZL
|
||||
{
|
||||
|
||||
TaskManager::TaskManager(size_t threadCount) {
|
||||
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 TaskManager::EnqueueBackgroundTask(std::function<void()> task) {
|
||||
boost::asio::post(ioContext, task);
|
||||
}
|
||||
|
||||
TaskManager::~TaskManager() {
|
||||
workGuard.reset(); // Ðàçðåøàåì ioContext.run() çàâåðøèòüñÿ, êîãäà çàäà÷ íå îñòàíåòñÿ
|
||||
ioContext.stop(); // Îïöèîíàëüíî: íåìåäëåííàÿ îñòàíîâêà
|
||||
for (auto& t : workers) {
|
||||
if (t.joinable()) t.join();
|
||||
}
|
||||
}
|
||||
|
||||
void MainThreadHandler::EnqueueMainThreadTask(std::function<void()> task) {
|
||||
std::lock_guard<std::mutex> lock(mainThreadMutex);
|
||||
mainThreadTasks.push(task);
|
||||
}
|
||||
|
||||
void MainThreadHandler::processMainThreadTasks() {
|
||||
std::function<void()> task;
|
||||
|
||||
// Èçâëåêàåì òîëüêî îäíó çàäà÷ó, ÷òîáû íå áëîêèðîâàòü update íàäîëãî
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(mainThreadMutex);
|
||||
if (!mainThreadTasks.empty()) {
|
||||
task = std::move(mainThreadTasks.front());
|
||||
mainThreadTasks.pop();
|
||||
}
|
||||
}
|
||||
|
||||
if (task) {
|
||||
task(); // Çäåñü âûïîëíÿåòñÿ RefreshVBO èëè çàãðóçêà òåêñòóðû
|
||||
}
|
||||
}
|
||||
}
|
||||
+20
-20
@@ -4,6 +4,8 @@
|
||||
#include <vector>
|
||||
#include <thread>
|
||||
#include <memory>
|
||||
#include <queue>
|
||||
|
||||
|
||||
namespace ZL {
|
||||
|
||||
@@ -14,30 +16,28 @@ namespace ZL {
|
||||
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();
|
||||
});
|
||||
}
|
||||
}
|
||||
//TaskManager(size_t threadCount = std::thread::hardware_concurrency());
|
||||
TaskManager(size_t threadCount = 2);
|
||||
|
||||
// Ìåòîä äëÿ äîáàâëåíèÿ ôîíîâîé çàäà÷è
|
||||
void EnqueueBackgroundTask(std::function<void()> task) {
|
||||
boost::asio::post(ioContext, task);
|
||||
}
|
||||
void EnqueueBackgroundTask(std::function<void()> task);
|
||||
|
||||
// Graceful shutdown
|
||||
~TaskManager() {
|
||||
workGuard.reset(); // Ðàçðåøàåì ioContext.run() çàâåðøèòüñÿ, êîãäà çàäà÷ íå îñòàíåòñÿ
|
||||
ioContext.stop(); // Îïöèîíàëüíî: íåìåäëåííàÿ îñòàíîâêà
|
||||
for (auto& t : workers) {
|
||||
if (t.joinable()) t.join();
|
||||
}
|
||||
}
|
||||
~TaskManager();
|
||||
};
|
||||
|
||||
|
||||
class MainThreadHandler
|
||||
{
|
||||
private:
|
||||
std::queue<std::function<void()>> mainThreadTasks;
|
||||
std::mutex mainThreadMutex;
|
||||
|
||||
public:
|
||||
void EnqueueMainThreadTask(std::function<void()> task);
|
||||
|
||||
// Âûïîëíåíèå çàäà÷ ïî îäíîé (èëè ïà÷êîé) çà êàäð
|
||||
void processMainThreadTasks();
|
||||
};
|
||||
|
||||
} // namespace ZL
|
||||
Reference in new issue
Block a user