Added ConstLockingPtr class.

git-svn-id: svn://svn.code.sf.net/p/loki-lib/code/trunk@498 7ec92016-0320-0410-acc4-a06ded1c099a
This commit is contained in:
rich_sposato 2006-01-19 19:36:34 +00:00
parent 963c55994f
commit 7acf9ff6be
2 changed files with 77 additions and 2 deletions

View file

@ -80,11 +80,73 @@ namespace Loki
}; // end class LockingPtr }; // end class LockingPtr
/** @class ConstLockingPtr
Similar to LockingPtr, except that it returns pointers and references to
a const SharedObject instead of a mutuable SharedObject.
@see LockingPtr
*/
template < typename SharedObject, typename LockingPolicy >
class ConstLockingPtr
{
public:
/** Constructor locks mutex associated with an object.
@param obj Reference to const object.
@param mtx Mutex used to control thread access to object.
*/
ConstLockingPtr( volatile const SharedObject & object,
LockingPolicy & mutex )
: pObject_( const_cast< const SharedObject * >( &object ) ),
pMutex_( &mutex )
{
mutex.Lock();
}
/// Destructor unlocks the mutex.
~ConstLockingPtr()
{
pMutex_->Unlock();
}
/// Star-operator dereferences pointer.
const SharedObject & operator * ()
{
return *pObject_;
}
/// Point-operator returns pointer to object.
const SharedObject * operator -> ()
{
return pObject_;
}
private:
/// Default constructor is not implemented.
ConstLockingPtr();
/// Copy-constructor is not implemented.
ConstLockingPtr( const ConstLockingPtr & );
/// Copy-assignment-operator is not implemented.
ConstLockingPtr & operator = ( const ConstLockingPtr & );
/// Pointer to the shared object.
const SharedObject * pObject_;
/// Pointer to the mutex.
LockingPolicy * pMutex_;
}; // end class ConstLockingPtr
} // namespace Loki } // namespace Loki
#endif // end file guardian #endif // end file guardian
// $Log$ // $Log$
// Revision 1.4 2006/01/19 19:34:19 rich_sposato
// Added ConstLockingPtr class.
//
// Revision 1.3 2006/01/16 18:34:37 rich_sposato // Revision 1.3 2006/01/16 18:34:37 rich_sposato
// Changed return type from LockingPtr to SharedObject. // Changed return type from LockingPtr to SharedObject.
// //

View file

@ -31,7 +31,7 @@ struct A
{ {
#define DO for(int i=0; i<10000000; i++) g++; #define DO for(int i=0; i<10000000; i++) g++;
void print(void* id) void print(void* id) const
{ {
DO;Printf("%p: ----------------\n")(id); DO;Printf("%p: ----------------\n")(id);
DO;Printf("%p: ---------------\n")(id); DO;Printf("%p: ---------------\n")(id);
@ -55,10 +55,11 @@ struct A
}; };
typedef Loki::LockingPtr<A,Loki::Mutex> LPtr; typedef Loki::LockingPtr<A,Loki::Mutex> LPtr;
typedef Loki::ConstLockingPtr<A,Loki::Mutex> CLPtr;
void* RunLocked(void *id) void* RunLocked(void *id)
{ {
A a; volatile A a;
static Loki::Mutex m; static Loki::Mutex m;
for(int i=0; i<loop; i++) for(int i=0; i<loop; i++)
{ {
@ -68,6 +69,18 @@ void* RunLocked(void *id)
return 0; return 0;
} }
void* RunConstLocked(void *id)
{
const volatile A a;
static Loki::Mutex m;
for(int i=0; i<loop; i++)
{
CLPtr l(a,m);
l->print(id);
}
return 0;
}
void* Run(void *id) void* Run(void *id)
{ {
A a; A a;