OSAPIMutex Class
The OSAPIMutex is used for simple mutual exclusion. More...
Header: | #include <OSAPI/Process/OSAPISemaphore.h> |
Public Functions
OSAPIMutex(bool disablePriorityInheritance = false, const char *pzName = nullptr) | |
virtual | ~OSAPIMutex() |
void | Destroy() |
bool | GetIsBinarySemaphore() const |
const char * | GetName() |
bool | IsLocked() |
bool | IsLockedByCurrentThread() const |
int | Lock(const char *strCaller = "") |
void | Release() |
void | SetName(const char *name) |
int | TryLock(int timeoutMs = 0, const char *strCaller = "") |
Static Public Members
int | GetGlobalMutexTimeout() |
void | SetGlobalMutexTimeout(const int nTimeout) |
Detailed Description
The OSAPIMutex is used for simple mutual exclusion.
Simple recursive mutex class for inter-thread use. Provides less overhead and simpler interface for synchronisation between threads running in same process space. By default the mutex is created in non-signaled state.
Usage
OSAPIMutex mutex; ... if(mutex.TryLock()) { mutex.Release(); } else // if not locked { mutex.Lock(); mutex.Release(); }
Features
- The mutex is recursive. A thread can call Lock() or TryLock() on a mutex that the thread already owns (has locked). Release() must be called as many times as Lock() was called.
- A mutex can only be released by the same thread that currently owns the mutex. When it is required to release the mutex from a different thread, the mutex should be replaced by a semaphore.
See also OSAPIMutexLocker.
Member Function Documentation
OSAPIMutex::OSAPIMutex(bool disablePriorityInheritance = false, const char *pzName = nullptr)
Constructs a mutex. Use bDisablePriorityInheritance to enable/disable priority inheritance in Linux.
[virtual]
OSAPIMutex::~OSAPIMutex()
Destructs the mutex.
void OSAPIMutex::Destroy()
Destroys the mutex and releases allocated resources. Automatically called by destructor.
[static]
int OSAPIMutex::GetGlobalMutexTimeout()
Accesses the global mutex timeout setting.
bool OSAPIMutex::GetIsBinarySemaphore() const
Checks if semaphore is binary.
A false return value also indicates that priority inheritance is enabled.
const char *OSAPIMutex::GetName()
bool OSAPIMutex::IsLocked()
Returns true
if mutex is locked, false
if not.
bool OSAPIMutex::IsLockedByCurrentThread() const
Checks if current thread has locked the mutex.
int OSAPIMutex::Lock(const char *strCaller = "")
Waits for signal if blocked, then takes ownership.
void OSAPIMutex::Release()
Frees ownership and unblocks.
Note that if a Windows thread calls LeaveCriticalSection when it does not have ownership of the specified critical section object, an error occurs that may cause another thread using EnterCriticalSection to wait indefinitely. Thus the variable isLocked for protection.
[static]
void OSAPIMutex::SetGlobalMutexTimeout(const int nTimeout)
Sets the global mutex timeout.
void OSAPIMutex::SetName(const char *name)
Sets name. Max 64 chars (char m_pzName[64]).
int OSAPIMutex::TryLock(int timeoutMs = 0, const char *strCaller = "")
Takes ownership if not blocked, otherwise waits until timeoutMs milliseconds. Returns 1
if ownership was granted.
Get started with CDP Studio today
Let us help you take your great ideas and turn them into the products your customer will love.