• Skip to main content
  • Skip to header right navigation
  • Skip to site footer
CDP Studio logo

CDP Studio

The no-code and full-code software development tool for distributed control systems and HMI

  • Why CDP
    • Software developers
    • Automation engineers
    • Managers
  • Product
    • Design UI
    • Develop
    • Analyze and test
    • Deploy
    • Framework and toolbox
    • Compatibility
  • Services
  • Use cases
  • Pricing
  • Try CDP

CDP Studio Documentation

  • Framework - CDP Core
  • OSAPIMutex
  • 4.11.14

OSAPIMutex Class

The OSAPIMutex is used for simple mutual exclusion. More...

Header: #include <OSAPI/Process/OSAPISemaphore.h>
  • List of all members, including inherited members

Public Functions

OSAPIMutex(bool disablePriorityInheritance = false, const char *pzName = nullptr)
virtual ~OSAPIMutex()
bool DebugVerifyCurrentThreadHasLockedTheMutex() const
void Destroy()
bool GetIsBinarySemaphore() const
const char *GetName()
bool IsLocked()
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.

bool OSAPIMutex::DebugVerifyCurrentThreadHasLockedTheMutex() const

Checks if current thread has locked 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.

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.

The content of this document is confidential information not to be published without the consent of CDP Technologies AS.

CDP Technologies AS, www.cdpstudio.com

Get started with CDP Studio today

Let us help you take your great ideas and turn them into the products your customer will love.

Try CDP Studio for free
Why CDP Studio?

CDP Technologies AS
Hundsværgata 8,
P.O. Box 144
6001 Ålesund, Norway

Tel: +47 990 80 900
E-mail: info@cdptech.com

Company

About CDP

Contact us

Services

Partners

Blog

Developers

Get started

User manuals

Support

Document download

Release notes

Follow CDP

  • LinkedIn
  • YouTube
  • GitHub

    © Copyright 2022 CDP Technologies. Privacy and cookie policy.

    Return to top