Protect KIID generator from threading

This commit is contained in:
Jon Evans 2021-11-21 13:30:36 -05:00
parent 016a98df86
commit 572747a3f9
1 changed files with 9 additions and 0 deletions

View File

@ -33,8 +33,12 @@
#include <boost/uuid/entropy_error.hpp> #include <boost/uuid/entropy_error.hpp>
#endif #endif
#include <mutex>
#include <wx/log.h> #include <wx/log.h>
// boost:mt19937 is not thread-safe
static std::mutex rng_mutex;
// Create only once, as seeding is *very* expensive // Create only once, as seeding is *very* expensive
static boost::mt19937 rng; static boost::mt19937 rng;
@ -69,9 +73,14 @@ KIID::KIID()
#endif #endif
if( createNilUuids ) if( createNilUuids )
{
m_uuid = nilGenerator(); m_uuid = nilGenerator();
}
else else
{
std::lock_guard<std::mutex> lock( rng_mutex );
m_uuid = randomGenerator(); m_uuid = randomGenerator();
}
#if BOOST_VERSION >= 106700 #if BOOST_VERSION >= 106700
} }