Mbed OS Reference
Loading...
Searching...
No Matches
ScopedLock< Lockable > Class Template Reference

RAII-style mechanism for owning a lock of Lockable object for the duration of a scoped block. More...

#include <ScopedLock.h>

Inheritance diagram for ScopedLock< Lockable >:
NonCopyable< ScopedLock< Lockable > >

Public Member Functions

 ScopedLock (Lockable &lockable)
 Locks given lockable object. More...
 

Detailed Description

template<typename Lockable>
class mbed::ScopedLock< Lockable >

RAII-style mechanism for owning a lock of Lockable object for the duration of a scoped block.

Template Parameters
LockableThe type implementing BasicLockable concept
Note
For type Lockable to be BasicLockable, the following conditions have to be satisfied:
  • has public member function lock which blocks until a lock can be obtained for the current execution context
  • has public member function unlock which releases the lock

Usage:

Example with rtos::Mutex

void foo(Mutex &m) {
// Mutex lock protects code in this block
}
RAII-style mechanism for owning a lock of Lockable object for the duration of a scoped block.
Definition: ScopedLock.h:63

More generic example

template<typename Lockable>
void foo(Lockable& lockable) {
ScopedLock<Lockable> lock(lockable);
// Code in this block runs under lock
}

Definition at line 63 of file ScopedLock.h.

Constructor & Destructor Documentation

◆ ScopedLock()

ScopedLock ( Lockable &  lockable)

Locks given lockable object.

Parameters
lockablereference to the instance of Lockable object
Note
lockable object should outlive the ScopedLock object

Definition at line 70 of file ScopedLock.h.