Boost logo

Boost Users :

From: Luc Bergeron (bluc_at_[hidden])
Date: 2002-10-17 14:12:25


Never mind my previous message... I found the solution after pressing
the send button.

Sorry.

Luc.

Luc Bergeron wrote:

> Hello everyone,
>
> i'm currently trying to implement a thread safe singleton using
> template. In the class I need to use a boost::mutex in order to protect
> access to the singleton pointer.
> The problem i'm currently encountering is that I need to initialize this
> static boost::mutex, but how !?!?!?!
>
> I tried using a pointer to a mutex and initialize it to null but that
> didn't work. I also tried to new it in the initialization but that
> failed too. :(
>
> Can someone help me on this one ???
>
> Thanks in advance for any help.
>
> Luc.
>
> P.S. I'm using boost 1.29 :)
>
> P.S.S Here is my current class definition : (A link to where I got this
> implementation :
> http://www.drizzle.com/~scottb/publish/gpgems1_singleton.htm)
> <http://www.drizzle.com/%7Escottb/publish/gpgems1_singleton.htm%29>
>
> template <typename T>
> class ThreadSafeSingleton
> {
> static T* d_ThreadSafeSingleton;
> static boost::mutex d_mutex;
>
> public:
> ThreadSafeSingleton()
> {
> assert(!d_ThreadSafeSingleton);
> int offset = (int)(T*)1 - (int)(ThreadSafeSingleton <T>*)(T*)1;
> d_ThreadSafeSingleton = (T*)((int)this + offset);
> }
> ~ThreadSafeSingleton()
> {
> assert(d_ThreadSafeSingleton);
> d_ThreadSafeSingleton = 0;
> }
>
> static T& GetSingleton(void)
> {
> boost::mutex::scoped_lock scoped_lock(d_mutex);
> assert(d_ThreadSafeSingleton);
> return *d_ThreadSafeSingleton;
> }
>
> static T* GetSingletonPtr(void)
> {
> boost::mutex::scoped_lock scoped_lock(d_mutex);
> return d_ThreadSafeSingleton;
> }
> };
>
> template <typename T> T* ThreadSafeSingleton
> <T>::d_ThreadSafeSingleton = 0;
>
>
> *Yahoo! Groups Sponsor*
> ADVERTISEMENT
> <http://rd.yahoo.com/M=237459.2482214.3895589.2146399/D=egroupweb/S=1705006788:HM/A=1278099/R=0/*http://www.verisign.com/cgi-bin/go.cgi?a=b30880107926004000>
>
>
>
> Info: <http://www.boost.org>
> Wiki: <http://www.crystalclearsoftware.com/cgi-bin/boost_wiki/wiki.pl>
> Unsubscribe: <mailto:boost-users-unsubscribe_at_[hidden]>
>
>
> Your use of Yahoo! Groups is subject to the Yahoo! Terms of Service
> <http://docs.yahoo.com/info/terms/>.

[Non-text portions of this message have been removed]


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