Boost logo

Boost :

Subject: [boost] [uuids] generating uuids from multiple threads
From: Michael Marcin (mike.marcin_at_[hidden])
Date: 2009-01-09 04:48:05


I currently have a shared
boost::uuids::basic_uuid_generator<boost::mt19937> which I protect by a
boost::mutex when I generate a new uuid.

The code looks something like:

std::wstring GetUniqueName()
{
     boost::uuids::uuid uniqueId;

     {
         boost::mutex::scoped_lock lock( my_mutex );
         uniqueId = my_generator();
     }

     return boost::lexical_cast<std::wstring>( uniqueId );
}

For one thing this is yet another case where I used a default
constructor on a uuid during natural coding and expected it to be cheap
but I digress.

Do I have to protect the shared generator with a mutex? Should I even be
using a shared generator or should I be constructing it on the stack in
this function? I don't expect this to be called very often (twice total
in our current application) but I want to make sure it is bullet proof
since it can potentially be called concurrently.

-- 
Michael Marcin

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk