Boost logo

Boost :

From: Tobias Schwinger (tschwinger_at_[hidden])
Date: 2008-01-16 14:44:20


thanks for your positive review. I almost overlooked it.

Phil Endecott wrote:
>> Today starts the formal Fast-Track review of the Boost.Utility/Singleton
>> library.
> I've never made much use of singletons, but I won't let that stop me
> from commenting...
> My thoughts:
> 1. There's no "un-synchronised" version, for use in single-threaded
> programs or when all (first) accesses come from the same thread.

If there is just a single thread all Singletons behave the same.

If you want to avoid the initialization check within one thread you can
'lease' a non-mutexed Singleton.

Yes, the creation will be synchronized but it's very unlikely that this
part will become a performance bottleneck.

> 2. The "mutexed access" feature can be useful in situation other than
> singletons; can't something more general-purpose be provided? I use a
> template class Lockable<T> which grants reader/writer-exclusive access
> to a contained T. (Reader/writer locking could be useful here.) (It
> might be useful to make the mutex type a template parameter, with a
> sensible default; I have a few mutexes of my own, with a
> boost::mutex-compatible interface.)

You can still use 'singleton<T>' and implement a custom synchronization
scheme. I think that's best if you want to use r/w locks effectively.

Further, a mutex can be more efficient than r/w locks; AFAIK those only
pay off if there's enough concurrency going on (I might be wrong).

> 3. Function static variables can be thought of as "local singletons",
> but they aren't synchronised. It would be great if we could replace
> "static T t(.....);" with "safe_static<T> t(.....);" and get the same semantics.

Is it implementable?


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