Boost logo

Boost :

Subject: [boost] [thread] Use of synchronized_value<> member in a constructor
From: Klaim - Joël Lamotte (mjklaim_at_[hidden])
Date: 2013-06-12 15:29:01

I am using now Boost 1.54.0 Beta (r84749).

I am writting code similar to the following (but a bit more complex) :

struct SequenceInfo
SequenceId id;
ProjectId project_id;
std::string name;

class Sequence
    boost::synchronized_value<SequenceInfo> m_info;


    explicit Sequence( SequenceId new_id, SequenceInfo info )
          : m_info( std::move(info) )
          m_info->id( new_id ); // mutex lock
// ...

My current understanding of concurrent access practice is that
it is not necessary to use synchronization mechanisms associated to member
manipulated into a constructor, because it can happen on only one thread.

If I am correct, then the lock of the synchronized_value is unnecessary.
I suspect that there might be other cases like this one where
the object responsable for the synchronized object should be able, rarely,
to access the object unsafely.

Suggestion: add an unsafe access to the object, in a very visible way.

    m_info.unsafe_access().id( new_id );


    m_info.unsafe_access()->id( new_id ); // using a smart pointer which
checks if safe accesses have been done inbetween and throw if it is the
case? or any other checks detecting potential error

It looks like it would allow avoiding cost where it could be avoided using
a mutex manipulated manually.

Any thoughts on this?

Joel Lamotte

Boost list run by bdawes at, gregod at, cpdaniel at, john at