mirror of
https://github.com/zhenyan121/Cubed.git
synced 2026-06-22 02:27:01 +08:00
feat(world): add dynamic thread pool resizing
This commit is contained in:
@@ -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}};
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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(); }
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user