#include "TaskManager.h" namespace ZL { TaskManager::TaskManager(size_t threadCount) { #ifndef EMSCRIPTEN workGuard = std::make_unique>(ioContext.get_executor()); for (size_t i = 0; i < threadCount; ++i) { workers.emplace_back([this]() { ioContext.run(); }); } #endif } void TaskManager::EnqueueBackgroundTask(std::function task) { #ifdef EMSCRIPTEN task(); #else boost::asio::post(ioContext, task); #endif } TaskManager::~TaskManager() { #ifndef EMSCRIPTEN workGuard.reset(); // Разрешаем ioContext.run() завершиться, когда задач не останется ioContext.stop(); // Опционально: немедленная остановка for (auto& t : workers) { if (t.joinable()) t.join(); } #endif } void MainThreadHandler::EnqueueMainThreadTask(std::function task) { #ifndef EMSCRIPTEN std::lock_guard lock(mainThreadMutex); #endif mainThreadTasks.push(task); } void MainThreadHandler::processMainThreadTasks() { std::function task; #ifdef EMSCRIPTEN if (!mainThreadTasks.empty()) { task = std::move(mainThreadTasks.front()); mainThreadTasks.pop(); } #else // Извлекаем только одну задачу, чтобы не блокировать update надолго { std::lock_guard lock(mainThreadMutex); if (!mainThreadTasks.empty()) { task = std::move(mainThreadTasks.front()); mainThreadTasks.pop(); } } #endif if (task) { task(); // Здесь выполняется RefreshVBO или загрузка текстуры } } }