Is there a standard template for protecting variables againstconcurrent access?

D

DerTopper

Hello newsgroup,

I need a template for protecting variables against concurrent access.
Is there a standard solution (boost or similar) for such a thing?

Thanks in advance,
Stuart
 
D

DerTopper


Thanks for your reply. Perhaps I didn't made myself clear enough. I
was looking for a template like the following (it uses MS's MFC
classes, so its not platform-independent, but you should get the
idea):

template<class t_Type>
class ProtectedVariable
{
public:
t_Type& operator= (const t_Type& Other)
{
// Lock the mutex first.
CSingleLock ProtectionMutexLock (&m_ProtectionMutex, TRUE);
m_Variable = Other;
return m_Variable;
}

t_Type get ()
{
// Lock the mutex first.
CSingleLock ProtectionMutexLock (&m_ProtectionMutex, TRUE);
return m_Variable;
}

protected:
t_Type m_Variable;
CMutex m_ProtectionMutex;
};

This template can then be used like this:
class SomeClass
{
public:
ProtectedVariable<double> DoubleVariable;
};

Actually, above class pretty much covers all I want, but
(A) it is not platform-independent and
(B) not part of any standard library.

Thanks,
Stuart
 
J

Juha Nieminen

Actually, above class pretty much covers all I want, but
(A) it is not platform-independent and
(B) not part of any standard library.

No mutex will be platform-independent nor standard (at least until the
next C++ standard comes out).

Just use Boost or OpenMP mutexes for your class.
 
D

DerTopper

  No mutex will be platform-independent nor standard (at least until the
next C++ standard comes out).

  Just use Boost or OpenMP mutexes for your class.

Thanks.
Stuart
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,241
Members
46,831
Latest member
RusselWill

Latest Threads

Top