feat(world): add thread pool size management and UI controls

This commit is contained in:
2026-06-21 16:59:11 +08:00
parent 34d9439466
commit 4f6c5303ec
4 changed files with 30 additions and 6 deletions

View File

@@ -48,6 +48,7 @@ private:
int m_pre_set_tick_speed = 1; int m_pre_set_tick_speed = 1;
bool m_tick_frezze = false; bool m_tick_frezze = false;
int m_samples_idx = 1; int m_samples_idx = 1;
int m_threads = 1;
void show_about_table_bar(); void show_about_table_bar();
void show_biome_table_bar(); void show_biome_table_bar();
void show_time_table_bar(); void show_time_table_bar();

View File

@@ -82,7 +82,8 @@ private:
std::atomic<bool> m_tick_running{true}; std::atomic<bool> m_tick_running{true};
std::atomic<int> m_rendering_distance{24}; std::atomic<int> m_rendering_distance{24};
std::atomic<float> m_chunk_gen_fraction{0.0f}; std::atomic<float> m_chunk_gen_fraction{0.0f};
std::atomic<int> m_pool_threads{1};
std::atomic<int> m_max_threads{1};
std::atomic<TickType> m_game_ticks{0}; std::atomic<TickType> m_game_ticks{0};
std::vector<ChunkPos> m_dirty_queue; std::vector<ChunkPos> m_dirty_queue;
@@ -159,7 +160,8 @@ public:
bool is_tick_running() const; bool is_tick_running() const;
void tick_running(bool run); void tick_running(bool run);
int pool_threads() const;
int max_threads() const;
void change_pool_threads(int threads); void change_pool_threads(int threads);
}; };

View File

@@ -463,6 +463,23 @@ void DevPanel::show_world_tab_item() {
if (ImGui::SliderInt("Render Distance", &rendering_distance, 2, 128)) { if (ImGui::SliderInt("Render Distance", &rendering_distance, 2, 128)) {
m_app.world().rendering_distance(rendering_distance); m_app.world().rendering_distance(rendering_distance);
} }
ImGui::Text(
"Pool Threads %d Max Support Threads %d Reserved Threads %d",
m_app.world().pool_threads(), m_app.world().max_threads(),
RESERVED_THREADS);
ImGui::SliderInt("Set Pool Threads", &m_threads, 1,
m_app.world().max_threads());
ImGui::SameLine();
if (ImGui::Button("Set")) {
m_app.world().change_pool_threads(m_threads);
}
if (m_threads > m_app.world().max_threads() - RESERVED_THREADS) {
ImGui::TextColored(
ImVec4(1.0f, 1.0f, 0.0f, 1.0f),
"Waring: When the threads in the thread pool exceed \n(maximum "
"threads minus reserved threads), \nit may cause stuttering.");
}
if (ImGui::Button("Rebuild World")) { if (ImGui::Button("Rebuild World")) {
m_app.world().rebuild_world(); m_app.world().rebuild_world();
} }

View File

@@ -657,13 +657,17 @@ void World::per_tick_time(int ms) { m_per_tick_time = ms; }
bool World::is_tick_running() const { return m_tick_running.load(); } bool World::is_tick_running() const { return m_tick_running.load(); }
void World::tick_running(bool run) { m_tick_running = run; } void World::tick_running(bool run) { m_tick_running = run; }
int World::pool_threads() const { return m_pool_threads.load(); }
int World::max_threads() const { return m_max_threads.load(); }
void World::change_pool_threads(int threads) { void World::change_pool_threads(int threads) {
int max_thread = std::thread::hardware_concurrency(); m_max_threads = std::thread::hardware_concurrency();
if (max_thread < 1) { if (m_max_threads < 1) {
max_thread = 4; Logger::warn("Can't Get Max Support Threads, Set Max Threads to 4");
m_max_threads = 4;
} }
int used_thread = std::clamp(threads, 1, max_thread); int used_thread = std::clamp(threads, 1, m_max_threads.load());
Logger::info("Create New Thread Pool Use {} Threads", used_thread); Logger::info("Create New Thread Pool Use {} Threads", used_thread);
m_gen_thread_pool.store(std::make_shared<ThreadPool>(used_thread)); m_gen_thread_pool.store(std::make_shared<ThreadPool>(used_thread));
m_pool_threads = used_thread;
} }
} // namespace Cubed } // namespace Cubed