[ create a new paste ] login | about

Link: http://codepad.org/PxDEGaFm    [ raw code | fork ]

Silex - C++, pasted on Jul 28:
#ifndef THREAD_SAFE_PTR
#define THREAD_SAFE_PTR

#include <boost/noncopyable.hpp>
#include <boost/thread/mutex.hpp>

template <class T>
class thread_safe_ptr : public boost::noncopyable
{

  public:

    class scoped_lock
    {

      public:

        explicit scoped_lock(const thread_safe_ptr& safe_ptr);

        T* operator->() const;

        T& operator*() const;


      private:

        const thread_safe_ptr& m_safe_ptr;
        boost::mutex::scoped_lock m_scoped_lock;

    };

    explicit thread_safe_ptr(T* pointer);

    scoped_lock operator->();

    scoped_lock operator*();

    T* get() const;

    void reset(T* ptr = 0);


  private:

    T* m_pointer;
    mutable boost::mutex m_mutex;

};

template <class T>
inline thread_safe_ptr<T>::scoped_lock::scoped_lock(const thread_safe_ptr& safe_ptr)
: m_safe_ptr(safe_ptr), m_scoped_lock(m_safe_ptr.m_mutex)
{
}

template <class T>
inline T* thread_safe_ptr<T>::scoped_lock::operator->() const
{
    return m_safe_ptr.get();
}

template <class T>
inline T& thread_safe_ptr<T>::scoped_lock:: operator*() const
{
    return *m_safe_ptr.get();
}

template <class T>
inline thread_safe_ptr<T>::thread_safe_ptr(T* pointer) : m_pointer(pointer)
{
}

template <class T>
inline typename thread_safe_ptr<T>::scoped_lock thread_safe_ptr<T>::operator->()
{
    return scoped_lock(*this);
}

template <class T>
inline typename thread_safe_ptr<T>::scoped_lock thread_safe_ptr<T>::operator*()
{
    return scoped_lock(*this);
}

template <class T>
inline T* thread_safe_ptr<T>::get() const
{
    return m_pointer;
}

template <class T>
inline void thread_safe_ptr<T>::reset(T* pointer)
{
    scoped_lock lock(this);
    m_pointer = pointer;
}

#endif


Create a new paste based on this one


Comments: