// ThreadsafeObject.h
// ThreadsafeObject by David Maisonave (609-345-1007) (www.Axter.com)
/ *
Description: ThreadsafeObject Is A Thread Safe Wrapper Class,
WHICH Uses Windows API critical-section logic.
IT CAN TURN Any Object Into A Thread Safe Object.
ThreadsafeObject Destructor Will Automatically Destroy the Object,
So The Object Can Be Create Via New On ThreadsafeObject Constructor.
This Insures No Other Process Has Ownership of the Object.
Example Usage
ThreadsafeObject
MythreadsaFevectorint.getlockedObject () -> Push_Back (123);
Cout << mythreadsafectorint.getlockedObject () -> at (0) << endl;
// An Instance of Reflock Can Be Create to Lock The Object for the duration of a function.
Example Usage
ThreadsafeObject
String somefunction ()
{
ThreadsafeObject
MyLockedString-> Operator = ("Hello World");
Return mylockedString-> Substr (6);
}
// The Destructor for Reflock Automatic Unlocks The Object, So An Explicit UNLOCK CALL
// is Not Required.
* /
#ifndef threadsafeObject_H_Header_Guard_
#define threadsafeObject_h_Header_Guard_
Template
Class threadsafeObject
{
PUBLIC:
ThreadsafeObject (T * Type): m_type (type) {INITIALIZECRITICALSECTION (& M_CS);
~ ThreadsafeObject ()
{
ENTERCRITICALSECTION (& M_CS);
DELETE M_TYPE; M_TYPE = NULL;
LeavecriticalSection; & M_CS);
DeletecriticalSection (& M_CS);
}
Class REFLOCK
{
PUBLIC:
REFLOCK (T * Type, critical_section & cs): m_type (type), m_cs (cs) {entercriticalsection (& m_cs);
Reflock (const reflock & src): m_type (src.m_type), m_cs (src.m_cs) {EntercriticalSection (& M_CS);} ~ reflick () {LeaveCriticalSection (& M_CS);
Inline T * Operator -> () const {return m_type;}
Inline T & Operator * () const {return m_type;}
T * m_type;
Private:
Critical_section & m_cs;
}
Reflock getLockedObject ()
{
Return REFLOCK (M_Type, M_CS);
}
Private:
T * m_type;
Critical_section m_cs;
ThreadsafeObject (Const ThreadsafeObject &);
ThreadsafeObject & Operator = (const threadsafeObject);
}
#ndif //! threadsafeObject_h_Header_Guard_