🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.cppbuilder.nativeapi
Article #6312

Re: threads memory access

#6312
From: Oliver Rutsch
Date: Wed, 25 May 2005 07:16
105 lines
4256 bytes
Hi Jonathan,

> 
> Thank you for this input. I'm going to try a critical section, and see 
> how it works out.
> 

If you intend to use critical sections then I have a class for you I 
wrote some time ago to bind a c.s. to a variable. I deleted my german 
comments and had no time yet to translate, sorry.
Here's an example how to use it:

Protect a double in a Thread:
Declaration:
TCritVar<double> MyCritVar;
Usage:
MyCritVar++;MyCritVar--;MyCritVar+=1.0; // Some simple thread-safe operators
double MyValue=MyCritVar.Get(); // Gets a copy of the object
MyCritVar.Set(3.0);  // Sets a new value

Protect a complex class in a thread, for example a double vector
Declaration:
TCritVar<vector<double> >MyCritVec;
Usage (complex operation):
{
   // Guard class locks and releases c.s. for MyCritVec
   // Or use MyCritVec->Lock() and ->Release() but take care of
   // exceptions -> deadlock possible!
   // Use the lock every time you read/write to/from the vector in your 
threads!
   TCritGuard cg(MyCritVec.GetCritSec());
   // GetUnsafe() gets a reference to the object. Use only with locked 
objects!
   MyCritVec.GetUnsafe().clear();
   MyCritVec.GetUnsafe().push_back(3.0);
}

Hope this helps a little bit.

Put this code in one of your headers:

#include <memory>
#include <SyncObjs.hpp>
/// Guard class for TCriticalSection
class TCritGuard
{
protected:
   TCriticalSection * mCritSec;
public:
   TCritGuard(TCriticalSection * const aCritSec):mCritSec(aCritSec)
                 {if (mCritSec) mCritSec->Acquire();};
   ~TCritGuard() {if (mCritSec) mCritSec->Release();};
};

template<class T> class TCritVar
{
protected:

   std::auto_ptr<TCriticalSection> mVarLock;

   T mVar;
public:

   TCritVar():mVar(){mVarLock.reset(new TCriticalSection());};

   TCritVar(const T & aVar){mVarLock.reset(new 
TCriticalSection());mVar=aVar;};

   T Get()const{TCritGuard VarGuard(mVarLock.get());return mVar;};

   void Set(const T & aVar){TCritGuard VarGuard(mVarLock.get());mVar=aVar;};

   T & GetUnsafe(){return mVar;};

   void Lock()const{mVarLock->Acquire();};

   void Release()const{mVarLock->Release();};

   TCriticalSection * const GetCritSec(){return mVarLock.get();};

   void operator= (const TCritVar & rhs){Set(rhs.mVar);};

   void operator+= (const T & rhs){TCritGuard 
VarGuard(mVarLock.get());mVar+=rhs;};

   void operator-= (const T & rhs){TCritGuard 
VarGuard(mVarLock.get());mVar-=rhs;};

   /// Atomically pre-increment <value_>.
   const T & operator++ (void){TCritGuard 
VarGuard(mVarLock.get());return ++mVar;};
   /// Atomically post-increment <value_>.
   const T operator++ (int){TCritGuard VarGuard(mVarLock.get());T 
tmp=mVar;++mVar;return tmp;};
   /// Atomically pre-decrement <value_>.
   const T & operator-- (void){TCritGuard 
VarGuard(mVarLock.get());return --mVar;};
   /// Atomically post-decrement <value_>.
   const T operator-- (int){TCritGuard VarGuard(mVarLock.get());T 
tmp=mVar;--mVar;return tmp;};
};

Bye,

Oliver
Technical Details
Message-ID: <42942668$1@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news3.google.com!news4.google.com!news2.google.com!newsread.com!news-xfer.newsread.com!news-out.newsgroups.com!propagator2-sterling.newsfeeds.com!in.nntp.be!caladan.landsraad.net!newsgroups.borland.com!not-for-mail
References: <428e4b3f$1@newsgroups.borland.com> <4291c5b1$1@newsgroups.borland.com> <4291ed05$1@newsgroups.borland.com> <429354a6@newsgroups.borland.com> <429356ac$1@newsgroups.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Thread: 10cc67,f662da50ec5f6f69
X-Google-Attributes: gid10cc67,public
X-Google-Language: ENGLISH,ASCII-7-bit
Date: Wed, 25 May 2005 09:16:40 +0200
From: Oliver Rutsch <orutsch@sympatec.com>
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.8) Gecko/20050511
X-Accept-Language: en-us, en, de-de
MIME-Version: 1.0
Newsgroups: borland.public.cppbuilder.nativeapi
Subject: Re: threads memory access
References: <428e4b3f$1@newsgroups.borland.com> <4291c5b1$1@newsgroups.borland.com> <4291ed05$1@newsgroups.borland.com> <429354a6@newsgroups.borland.com> <429356ac$1@newsgroups.borland.com>
In-Reply-To: <429356ac$1@newsgroups.borland.com>
Content-Type: text/plain; charset=us-ascii; format=flowed
Content-Transfer-Encoding: 7bit
NNTP-Posting-Host: 217.5.250.34
Message-ID: <42942668$1@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1117005416 217.5.250.34 (25 May 2005 00:16:56 -0700)
Lines: 105
Path: arc1-ca.pugleaf.net!g2news1.google.com!news3.google.com!news4.google.com!news2.google.com!newsread.com!news-xfer.newsread.com!news-out.newsgroups.com!propagator2-sterling.newsfeeds.com!in.nntp.be!caladan.landsraad.net!newsgroups.borland.com!not-for-mail