Boost logo

Boost Users :

Subject: Re: [Boost-users] [random] Singleton engine
From: Rutger ter Borg (rutger_at_[hidden])
Date: 2009-07-29 10:44:52


jp wrote:

[snip]
> The problem I have with my implementation is that what I really want
> is a thread-local singleton. If I have to make the access threadsafe,
> then I may be better off recreating it every time. Any ideas on how
> best to do this? Could it be done with the boost::serialization
> singleton? My guess is that it may be possible by using the loki
> creation policy, but I have no idea how to go about doing it.

Perhaps using http://tinyurl.com/m2pukz? I.e., something like below
(untested)?

Kind regards,

Rutger

template< typename Singleton >
Singleton& get_singleton() {
  static boost::thread_specific_ptr< Singleton > m_singleton;
  if ( !m_singleton.get() ) {
    m_singleton.reset( new Singleton() );
  }
  return *m_singleton;
}


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net