fix: data race in world::init_world()

This commit is contained in:
2026-04-18 15:14:11 +08:00
parent 11b6e88d0d
commit de4df4b476
6 changed files with 71 additions and 25 deletions

View File

@@ -2,17 +2,32 @@
#include <Cubed/tools/log.hpp>
#include <atomic>
namespace Cubed {
unsigned Random::get_base_seed() {
static unsigned base = [] {
std::random_device rd;
return rd();
}();
return base;
}
unsigned Random::get_thread_seed() {
static std::atomic<unsigned> counter{0};
thread_local static unsigned seed = get_base_seed() + counter.fetch_add(1);
return seed;
}
Random::Random() {
std::random_device d;
m_seed = d();
m_seed = get_thread_seed();
Logger::info("Seed: {}", m_seed);
m_engine.seed(m_seed);
}
Random& Random::get() {
static Random instance;
thread_local Random instance;
return instance;
}