boost::uuids::random_generator and uniqueness with multiple threads

后端 未结 1 2040

When I generate the random number with single thread, no duplicate in 4M uuids generated but if I generate with two threads each 1M, I see roughly 16-20 duplicates. What could

1条回答
  •  情歌与酒
    2021-02-15 17:07

    This is a common error when using RAII locks: you forgot to give your lock a name in the line

          boost::mutex::scoped_lock(m_mRandomGen);
    

    so it didn't lock anything at all. Change it to

          boost::mutex::scoped_lock lk(m_mRandonGen); // note the typo in mutex name
    

    EDIT: what really happened: There was no compiler error despite the typo in the mutex name because the declaration

    type(name);
    

    is the same as

    type name;
    

    if the name has not been declared before. In other words, you've default-constructed a new scoped_lock called m_mRandomGen, not associated with a mutex.

    0 讨论(0)
提交回复
热议问题