Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Mutex Class Reference
[Mutex class]
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 (uint32_t millisec=osWaitForever) |
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 | 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.
- 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 will be created on 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 65 of file Mutex.h.
Constructor & Destructor Documentation
Mutex | ( | ) |
Mutex | ( | const char * | name ) |
~Mutex | ( | ) |
Member Function Documentation
osThreadId get_owner | ( | ) |
osStatus lock | ( | uint32_t | millisec = osWaitForever ) |
Wait until a Mutex becomes available.
- Parameters:
-
millisec timeout value or 0 in case of no time-out. (default: osWaitForever)
- 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. osErrorParameter internal error. osErrorResource the mutex could not be obtained when no timeout was specified. osErrorISR this function cannot be called from the interrupt service routine.
- Note:
- You cannot call this function from ISR context.
bool trylock | ( | ) |
bool trylock_for | ( | uint32_t | millisec ) |
Try to lock the mutex for a specified time.
- Parameters:
-
millisec timeout value or 0 in case of no time-out.
- 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:
-
millisec absolute 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 | ( | void | ) |
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. osErrorParameter internal error. osErrorResource the mutex was not locked or the current thread wasn't the owner. osErrorISR this function cannot be called from the interrupt service routine.
- Note:
- You cannot call this function from ISR context.
Generated on Tue Jul 12 2022 14:27:16 by
