Mutex Locker utility class to lock/unlock mbed RTOS mutexes. Create and pass a mutex, it locks the mutex then unlocks it when destroyed.

Dependents:   SmartLight HelloThreadsYo

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers mutexlocker.h Source File

mutexlocker.h

00001 /* (C) 2014 Richard Thompson (Tomo2k)
00002 This software is Apache 2.0 licenced for this to be reused.
00003 
00004 That said, it's trivial and rather obvious!
00005 */
00006 #pragma once
00007 
00008 #include "rtos.h"
00009 
00010 
00011 //! Lock/Unlock Mutex using variable scope.
00012 //! Ensures always unlocked regardless of method exit
00013 class MutexLocker
00014 {
00015 public:
00016     //! Lock the mutex
00017     MutexLocker(Mutex &mutex) :
00018         m_mutex(mutex) {
00019         m_mutex.lock();
00020     }
00021     //! Unlocks on destruction
00022     ~MutexLocker() {
00023         m_mutex.unlock();
00024     }
00025 private:
00026     Mutex &m_mutex;
00027     // Disable copy
00028     MutexLocker & operator=(const MutexLocker&);
00029     MutexLocker(const MutexLocker &);
00030 };