feat(world): add dynamic thread pool resizing

This commit is contained in:
2026-06-21 16:36:15 +08:00
parent 2386d98217
commit 34d9439466
4 changed files with 46 additions and 25 deletions

View File

@@ -26,7 +26,7 @@ constexpr float DEFAULT_G = 22.5f;
constexpr int SIZE_X = CHUNK_SIZE;
constexpr int SIZE_Y = WORLD_SIZE_Y;
constexpr int SIZE_Z = CHUNK_SIZE;
constexpr int RESERVED_THREADS = 3;
constexpr ChunkPos CHUNK_DIR[]{{1, 0}, {-1, 0}, {0, 1}, {0, -1},
{1, 1}, {-1, 1}, {1, -1}, {-1, -1}};

View File

@@ -58,7 +58,7 @@ private:
std::thread m_gen_thread;
std::thread m_server_thread;
std::unique_ptr<ThreadPool> m_gen_thread_pool;
std::atomic<std::shared_ptr<ThreadPool>> m_gen_thread_pool;
std::stop_source m_server_stop_source;
std::atomic<int> m_per_tick_time = DEFAULT_PER_TICK_TIME; // ms
@@ -159,6 +159,8 @@ public:
bool is_tick_running() const;
void tick_running(bool run);
void change_pool_threads(int threads);
};
} // namespace Cubed

View File

@@ -43,20 +43,7 @@ public:
});
}
}
~ThreadPool() {
m_stopping = true;
for (auto& w : m_workers) {
w.request_stop();
}
m_cv.notify_all();
for (auto& w : m_workers) {
if (w.joinable()) {
w.join();
}
}
}
~ThreadPool() { stop(); }
template <typename F> auto enqueue(F&& f) {
using R = std::invoke_result_t<F>;
@@ -74,6 +61,20 @@ public:
m_cv.notify_one();
return fut;
}
void stop() {
m_stopping = true;
for (auto& w : m_workers) {
w.request_stop();
}
m_cv.notify_all();
for (auto& w : m_workers) {
if (w.joinable()) {
w.join();
}
}
}
size_t thread_sum() const { return m_thread_sum.load(); }
};