qpid::sys::LockPtr< T, Lock > Class Template Reference

LockPtr is a smart pointer to T. More...

#include <LockPtr.h>

List of all members.

Public Member Functions

 LockPtr (volatile T *p, Lock &l)
 LockPtr (volatile T *p, volatile Lock &l)
T & operator* ()
T * operator-> ()


Detailed Description

template<class T, class Lock>
class qpid::sys::LockPtr< T, Lock >

LockPtr is a smart pointer to T.

It is constructed from a volatile T* and a Lock (by default a Mutex). It const_casts away the volatile qualifier and locks the Lock for the duration of its

Used in conjuntion with the "volatile" keyword to get the compiler to help enforce correct concurrent use of mutli-threaded objects. See ochttp://www.ddj.com/cpp/184403766 for a detailed discussion.

To summarize the convention:

This means that code calling on a concurrently-used object (declared volatile) can only call thread-safe (volatile) member functions. Code that needs to use thread-unsafe members must use a LockPtr, thereby acquiring the lock and making it safe to do so.

A good type-safe pattern is the internally-locked object:

This encapsulates all the locking logic inside the class.

One nice feature of this convention is the common case where you need a public, locked version of some function foo() and also a private unlocked version to avoid recursive locks. They can be declared as volatile and non-volatile overloads of the same function:

// public void Thing::foo() volatile { LockPtr<Thing>(this, myLock)->foo(); } // private void Thing::foo() { ... do stuff ...}

Definition at line 71 of file LockPtr.h.


The documentation for this class was generated from the following file:

Generated on Thu Oct 16 13:38:39 2008 for Qpid by  doxygen 1.5.6