Mistake on this page?
Report an issue in GitHub or email us
Public Member Functions
Mutex Class Reference

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

#include <Mutex.h>

Inheritance diagram for Mutex:
NonCopyable< Mutex >

Public Member Functions

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

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.

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 66 of file Mutex.h.

Constructor & Destructor Documentation

Mutex ( )

Create and Initialize a Mutex object.

Note
You cannot call this function from ISR context.
Mutex ( const char *  name)

Create and Initialize a Mutex object.

Parameters
namename to be used for this mutex. It has to stay allocated for the lifetime of the thread.
Note
You cannot call this function from ISR context.
~Mutex ( )

Mutex destructor.

Note
You cannot call this function from ISR context.

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.
osStatus lock ( void  )

Wait until a Mutex becomes available.

Returns
status code that indicates the execution status of the function: osOK the mutex has been obtained.
Note
You cannot call this function from ISR context.
This function treats RTOS errors as fatal system errors, so it can only return osOK. Use of the return value is deprecated, as the return is expected to become void in the future.
osStatus lock ( uint32_t  millisec)

Wait until a Mutex becomes available.

Deprecated:
Do not use this function. This function has been replaced with lock(), trylock() and trylock_for() functions.
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.
bool trylock ( )

Try to lock the mutex, and return immediately.

Returns
true if the mutex was acquired, false otherwise.
Note
equivalent to trylock_for(0)
You cannot call this function from ISR context.
bool trylock_for ( uint32_t  millisec)

Try to lock the mutex for a specified time.

Parameters
millisectimeout value.
Returns
true if the mutex was acquired, false otherwise.
Note
the underlying RTOS may have a limit to the maximum wait time due to internal 32-bit computations, but this is guaranteed to work if the wait is <= 0x7fffffff milliseconds (~24 days). If the limit is exceeded, the lock attempt will time out earlier than specified.
You cannot call this function from ISR context.
bool trylock_until ( uint64_t  millisec)

Try to lock the mutex until specified time.

Parameters
millisecabsolute timeout time, referenced to Kernel::get_ms_count()
Returns
true if the mutex was acquired, false otherwise.
Note
the underlying RTOS may have a limit to the maximum wait time due to internal 32-bit computations, but this is guaranteed to work if the wait is <= 0x7fffffff milliseconds (~24 days). If the limit is exceeded, the lock attempt will time out earlier than specified.
You cannot call this function from ISR context.
osStatus unlock ( )

Unlock the mutex that has previously been locked by the same thread.

Returns
status code that indicates the execution status of the function: osOK the mutex has been released.
Note
You cannot call this function from ISR context.
This function treats RTOS errors as fatal system errors, so it can only return osOK. Use of the return value is deprecated, as the return is expected to become void in the future.
Important Information for this Arm website

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies. If you are not happy with the use of these cookies, please review our Cookie Policy to learn how they can be disabled. By disabling cookies, some features of the site will not work.