Kenji Arai / mbed-os_TYBLE16

Dependents:   TYBLE16_simple_data_logger TYBLE16_MP3_Air

Embed: (wiki syntax)

« Back to documentation index

Mutex Class Reference

The Mutex class is used to synchronize the execution of threads. More...

#include <Mutex.h>

Inherits NonCopyable< Mutex >.

Public Member Functions

 Mutex ()
 Create and Initialize a Mutex object.
 Mutex (const char *name)
 Create and Initialize a Mutex object.
osStatus lock ()
 Wait until a Mutex becomes available.
 MBED_DEPRECATED_SINCE ("mbed-os-5.10.0","Replaced with lock(), trylock() and trylock_for() functions") osStatus lock(uint32_t millisec)
 Wait until a Mutex becomes available.
bool trylock ()
 Try to lock the mutex, and return immediately.
bool trylock_for (uint32_t millisec)
 Try to lock the mutex for a specified time.
bool trylock_until (uint64_t millisec)
 Try to lock the mutex until specified time.
osStatus unlock ()
 Unlock the mutex that has previously been locked by the same thread.
osThreadId_t get_owner ()
 Get the owner the this mutex.
 ~Mutex ()
 Mutex destructor.

Private Member Functions

 MBED_DEPRECATED ("Invalid copy construction of a NonCopyable resource.") NonCopyable(const NonCopyable &)
 NonCopyable copy constructor.
 MBED_DEPRECATED ("Invalid copy assignment of a NonCopyable resource.") NonCopyable &operator
 NonCopyable copy assignment operator.

Detailed Description

The Mutex class is used to synchronize the execution of threads.

This is, for example, used to protect access to a shared resource.

In bare-metal builds, the Mutex class is a dummy, so lock() and unlock() are no-ops.

Note:
You cannot use member functions of this class in ISR context. If you require Mutex functionality within ISR handler, consider using Semaphore.
Memory considerations: The mutex control structures are created on the current thread's stack, both for the Mbed OS and underlying RTOS objects (static or dynamic RTOS memory pools are not being used).

Definition at line 68 of file Mutex.h.


Member Function Documentation

osThreadId get_owner (  )

Get the owner the this mutex.

Returns:
the current owner of this mutex.
Note:
You cannot call this function from ISR context.

Definition at line 143 of file Mutex.cpp.

MBED_DEPRECATED_SINCE ( "mbed-os-5.10.0"  ,
"Replaced with   lock(),
trylock() and trylock_for() functions"   
)

Wait until a Mutex becomes available.

Parameters:
millisectimeout value.
Returns:
status code that indicates the execution status of the function: osOK the mutex has been obtained. osErrorTimeout the mutex could not be obtained in the given time. osErrorResource the mutex could not be obtained when no timeout was specified.
Note:
You cannot call this function from ISR context.
This function treats RTOS errors as fatal system errors, so it can only return osOK or osErrorResource in case when millisec is 0 or osErrorTimeout if millisec is not osWaitForever.