From 4c4cc515a4564040f10263471ceeb10792930395 Mon Sep 17 00:00:00 2001 From: zhenyan121 <3367366583@qq.com> Date: Sun, 21 Jun 2026 18:39:40 +0800 Subject: [PATCH] refactor(world): add thread pool start/stop and auto-detect threads Extract thread pool starting and stopping into dedicated methods. Set default pool threads to 0 to enable automatic detection of available cores, and ensure thread pool is properly managed during world rebuild. --- include/Cubed/gameplay/world.hpp | 4 +++- src/gameplay/world.cpp | 34 +++++++++++++++++++++----------- 2 files changed, 25 insertions(+), 13 deletions(-) diff --git a/include/Cubed/gameplay/world.hpp b/include/Cubed/gameplay/world.hpp index 0cc926f..1d22473 100644 --- a/include/Cubed/gameplay/world.hpp +++ b/include/Cubed/gameplay/world.hpp @@ -84,7 +84,7 @@ private: std::atomic m_tick_running{true}; std::atomic m_rendering_distance{24}; std::atomic m_chunk_gen_fraction{0.0f}; - std::atomic m_pool_threads{1}; + std::atomic m_pool_threads{0}; std::atomic m_max_threads{1}; std::atomic m_game_ticks{0}; std::atomic m_chunk_load_style{ChunkLoadStyle::RANDOM}; @@ -146,6 +146,8 @@ public: void start_server_thread(); void stop_gen_thread(); void stop_server_thread(); + void stop_thread_pool(); + void start_thread_pool(); void serever_run(std::stop_token stoken); CaveCarver& cave_carcer(); diff --git a/src/gameplay/world.cpp b/src/gameplay/world.cpp index 63d4fd7..b4828e5 100644 --- a/src/gameplay/world.cpp +++ b/src/gameplay/world.cpp @@ -23,11 +23,7 @@ World::~World() { stop_gen_thread(); stop_server_thread(); wait_all_chunk_tasks(); - auto pool_ptr = m_gen_thread_pool.load(); - if (pool_ptr) { - pool_ptr->stop(); - } - m_gen_thread_pool.store(nullptr); + stop_thread_pool(); m_chunks.clear(); { @@ -90,8 +86,7 @@ void World::init_world() { m_cave_carcer.init(ChunkGenerator::seed()); m_river_worm.init(ChunkGenerator::seed()); m_chunks.reserve(MAX_DISTANCE * MAX_DISTANCE * 4); - int max_thread = std::thread::hardware_concurrency(); - change_pool_threads(max_thread - RESERVED_THREADS); + start_thread_pool(); auto t1 = std::chrono::system_clock::now(); @@ -139,11 +134,6 @@ void World::gen_chunks_internal() { // Logger::info("gen_chunks_internal"); m_chunk_gen_fraction = 0.0f; m_chunk_gen_finished = false; - /* - if (!new_chunks.empty()) { - submit_new_chunks(); - return; - }*/ ChunkPosSet required_chunks; ChunkPairVector temp_neighbor; @@ -366,6 +356,24 @@ void World::stop_server_thread() { } } +void World::stop_thread_pool() { + auto pool_ptr = m_gen_thread_pool.load(); + if (pool_ptr) { + pool_ptr->stop(); + } + m_gen_thread_pool.store(nullptr); + Logger::info("Thread Pool Stopped"); +} + +void World::start_thread_pool() { + int max_thread = std::thread::hardware_concurrency(); + if (m_pool_threads == 0) { + change_pool_threads(max_thread - RESERVED_THREADS); + } else { + change_pool_threads(m_pool_threads); + } +} + void World::serever_run(std::stop_token stoken) { Logger::info("Server Thread Started!"); while (!stoken.stop_requested()) { @@ -616,6 +624,7 @@ void World::rebuild_world() { } m_is_rebuilding = true; stop_gen_thread(); + stop_thread_pool(); m_cave_carcer.reload(ChunkGenerator::seed()); m_river_worm.reload(ChunkGenerator::seed()); { @@ -625,6 +634,7 @@ void World::rebuild_world() { } m_could_gen = true; ChunkGenerator::reload(); + start_thread_pool(); start_gen_thread(); need_gen();