TIP: Click on subject to list as thread! ANSI
echo: c_plusplus
to: JERRY COFFIN
from: PETER GARNER
date: 1997-09-12 19:41:00
subject: volatile objects?

One other way to do thread safe objects, is to declare the following
classes :
class Mutex ;
class MutexLock ;
class SerialObject ;   //  A Thread Safe Object
The classes are implemented as follows :
class Mutex  //  This is not OS specific, I am using general keywords
{
public  :
  int lock () { return os_lock ( os_mutex ) ; } // Whatever your OS Mutex 
lock fcn is
  int unlock () { return os_unlock ( os_mutex ) ; }  // Whatever your OS 
Mutex unlock fcn is
private :
  OpSysMutexSem  os_mutex    ;    //  However your OS declares Mutexes
}
//  Constructor Lock a mutex, destructor unlocks it.
class MutexLock
{
public :
  MutexLock ( Mutex & rmutex ) : rmutexmem ( rmutex ) { rmutexmem.lock () ; }
  ~MutexLock () { rmutexmem.unlock () ; }
private :
  MutexMember & rmutexmem ; //  OUR mutex class declared above.
}
class ThreadObject
{
protected  :
  static Mutex & getMutex () { return rmutex ; }
private :
  static Mutex mutex ;
}
class ThreadChild : public ThreadObject
{
  ThreadSafeMethod
  {
     //  Get the lock.
     MutexLock mulock ( getMutex () ) ;
     //  Do any thread safe stuff after the MutexLock is constructed.  Since 
all descendents share the same
     //  static MutexLock, they are all serialized, and if an exception is 
thrown, the MutexLock destructor will
     //  automatically free the mutex.  No worries there!
    //  End of thread safe member method, MutexLock destructor frees the 
mutex automatically
  }
}
This is (roughly) how the IBM OCL framework implements IResource and
IResourceLock to serialize Threadsafe Objects.  We implemented something
similiar under AIX where the OCL classes are declared but do not work!
This is safe since if follows Stroustrups' "Resource Allocation by
Acquisition" method whereby the destructor automatically frees the
resource when it goes out of scope.  This alleviates concerns caused by
functions having more than one return statement and throwing exceptions.
Thanks
Peter
---
 þ KWQ/2 1.2i þ If at first you don't succeed, put it out for beta test.
---------------
* Origin: St. Louis Users Group (1:100/4)

SOURCE: echomail via exec-pc

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.