EtcPal  0.4.1
ETC Platform Abstraction Layer (EtcPal)
View other versions:
Mutex Class Reference

Overview

A wrapper class for the EtcPal mutex type.

Note: The etcpal::Mutex functions are not normally used directly - prefer usage of the RAII type etcpal::MutexGuard to manage locking and Unlocking of an etcpal::Mutex.

Example usage:

static int shared_counter;
void CounterThread()
{
for (int i = 0; i < 10000; ++i)
{
etcpal::MutexGuard lock(mutex);
++shared_counter;
}
}
int main()
{
std::vector<etcpal::Thread> counter_threads;
counter_threads.reserve(5);
for (int i = 0; i < 5; ++i)
{
counter_threads.emplace_back(CounterThread);
}
// Wait for the threads to finish
for (auto& thread : counter_threads)
{
thread.Join();
}
std::cout << "The counter is: " << shared_counter << '\n'; // Prints "the counter is: 50000"
return 0;
}
Lock guard around a mutex.
Definition: mutex.h:173
A wrapper class for the EtcPal mutex type.
Definition: mutex.h:88
static void Sleep(unsigned int ms) noexcept
Blocks the current thread for the specified number of milliseconds.
Definition: thread.h:491

See mutex (Mutexes) for more information.

Public Member Functions

 Mutex ()
 Create a new mutex.
 
 ~Mutex ()
 Destroy the mutex.
 
 Mutex (const Mutex &other)=delete
 
Mutexoperator= (const Mutex &other)=delete
 
 Mutex (Mutex &&other)=delete
 
Mutexoperator= (Mutex &&other)=delete
 
bool Lock ()
 Lock the mutex. More...
 
bool TryLock (int timeout_ms=0)
 Attempt to lock the mutex. More...
 
void Unlock ()
 Unlock the mutex. More...
 
etcpal_mutex_tget ()
 Get a reference to the underlying etcpal_mutex_t type.
 

Member Function Documentation

◆ Lock()

bool Lock ( )
inline

Lock the mutex.

Returns
The result of etcpal_mutex_lock() on the underlying mutex.

◆ TryLock()

bool TryLock ( int  timeout_ms = 0)
inline

Attempt to lock the mutex.

Returns when either the mutex is acquired or the timeout expires. NOTE: Timeout values other than 0 or ETCPAL_WAIT_FOREVER are typically only honored on real-time platforms. See the table in mutex (Mutexes) for more information. On platforms where timeouts are not honored, passing 0 for timeout_ms executes a poll for the mutex returning immediately, while any other value executes the equivalent of Lock().

Parameters
timeout_msHow long to wait to acquire the mutex, in milliseconds. Default is to poll and return immediately.
Returns
The result of etcpal_mutex_timed_lock() on the underlying mutex.

◆ Unlock()

void Unlock ( )
inline

Unlock the mutex.

See etcpal_mutex_Unlock().


The documentation for this class was generated from the following file: