mirror of
https://github.com/zhenyan121/Cubed.git
synced 2026-06-18 00:27:02 +08:00
fix: data race in world::init_world()
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user