Base Framework
|
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 | |
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:
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:
|
inlineexplicitnoexcept |
Initializes the synchronization object.
object | The synchronize able object to be synchronized. |
exclusive | Specifies if the lock should be exclusive (write-lock) or shared (read-lock). Default is exclusive. |
|
inlinenoexcept |
Forces the lock to be released. Releases the lock if not already released and destroys the synchronization object.