#include <LockPtr.h>
Public Member Functions | |
LockPtr (volatile T *p, Lock &l) | |
LockPtr (volatile T *p, volatile Lock &l) | |
T & | operator* () |
T * | operator-> () |
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.