Base Framework
Public Member Functions | List of all members
Synchronize< LOCK > Class Template Reference

A stack based wrapper of a synchronize able class. More...

#include <base/concurrency/Synchronize.h>

Public Member Functions

 Synchronize (const Synchronizeable< LOCK > &_object, bool exclusive=true) noexcept
 
 ~Synchronize () noexcept
 

Detailed Description

template<class LOCK>
class Synchronize< LOCK >

A stack based wrapper of a synchronize able class.

This is a stack based wrapper of a synchronize able class. A Synchronize object will acquire and release an exclusive (or a shared lock) on the provided synchronize able object when it is, respectively, created and destroyed. If required, the lock can be explicitly released by calling the method release(). This is required before throwing an exception.

Three macros have been provided for easy use of this class: SynchronizeExclusively(), SynchronizeShared(), and SynchronizeRelease(). These macros should be used like any normal function.

SynchronizeExclusively(): Selects exclusively synchronization of the current scope within a synchronize able class (Synchronizeable). The macro manages this by creating a temporary Synchronize object called '_sync' on the stack. Please note that the variable '_sync' is reserved for this purpose and should not be used elsewhere to avoid misunderstandings. You should only use the provided macros to access the variable '_sync'.

SynchronizeShared(): Works the same way as SynchronizeExclusively() but, as the name suggests, acquires a shared lock.

SynchronizeRelease(): If required, you can release the lock explicitly by using SynchronizeRelease().

The macros are intended to be used within a template class that takes a template argument called LOCK and is a base class of Synchronizeable<LOCK>. Like illustrated by this example:

template<class LOCK = DefaultLock>
class MyClass : public Synchronizeable<LOCK> {
// stuffing goes here :-)
};

However, if you for some reason want to use these macros within a class that does not take a template argument LOCK, you must define LOCK explicitly and identical to the template argument of the base class Synchronizeable. Like this:

class MyClass : public Synchronizeable<DefaultLock> {
public:
typedef DefaultLock LOCK;
void myFirstMethod() const noexcept
{
SynchronizeShared();
// do something as long as it doesn't modify the object
}
void mySecondMethod() noexcept
{
SynchronizeExclusive();
// do modification of object
}
};
See also
Synchronizeable
Version
1.0

Constructor & Destructor Documentation

◆ Synchronize()

template<class LOCK >
Synchronize< LOCK >::Synchronize ( const Synchronizeable< LOCK > &  _object,
bool  exclusive = true 
)
inlineexplicitnoexcept

Initializes the synchronization object.

Parameters
objectThe synchronize able object to be synchronized.
exclusiveSpecifies if the lock should be exclusive (write-lock) or shared (read-lock). Default is exclusive.

◆ ~Synchronize()

template<class LOCK >
Synchronize< LOCK >::~Synchronize ( )
inlinenoexcept

Forces the lock to be released. Releases the lock if not already released and destroys the synchronization object.

Synchronizeable
Wrapper for a synchronization object.
Definition: Synchronizeable.h:58
Unsafe
Select no synchronization.
Definition: Synchronizeable.h:35