Beamlib  3.0.1
This is the Beam C++ class library.
BMutex.h
Go to the documentation of this file.
1 /*******************************************************************************
2  * BMutex.h BMutex Classes
3  * T.Barnaby, BEAM Ltd, 1/11/02
4  * Copyright (c) 2022 All Right Reserved, Beam Ltd, https://www.beam.ltd.uk
5  * For license see LICENSE.txt at the root of the beamlib source tree.
6  *******************************************************************************
7  */
8 #ifndef BMUTEX_H
9 #define BMUTEX_H 1
10 
11 #include <pthread.h>
12 
14 class BMutex {
15 public:
16  enum Type { Normal, Recursive };
17 
18  BMutex(Type type = Normal);
19  BMutex(const BMutex& mutex);
20  ~BMutex();
21 
22  int lock();
23  int timedLock(int timeoutUs);
24  int unlock();
25  int tryLock();
26 
27  BMutex& operator=(const BMutex& mutex);
28 private:
29  pthread_mutex_t omutex;
30 };
31 
33 class BMutexLock {
34 public:
35  BMutexLock(BMutex& lock, int doLock = 0) : olock(lock) { if(doLock) olock.lock(); }
36  ~BMutexLock() { olock.unlock(); }
37  int lock() { return olock.lock(); }
38  int unlock() { return olock.unlock(); }
39 private:
40  BMutex& olock;
41 };
42 #endif
Mutex class that removes the lock on deletion and so is useful to lock data in a function call.
Definition: BMutex.h:33
int unlock()
Definition: BMutex.h:38
~BMutexLock()
Definition: BMutex.h:36
BMutexLock(BMutex &lock, int doLock=0)
Definition: BMutex.h:35
int lock()
Definition: BMutex.h:37
Mutex class. Note these are recursive Mutexes and so you need to make sure the number of unlocks equa...
Definition: BMutex.h:14
Type
Definition: BMutex.h:16
@ Normal
Definition: BMutex.h:16
@ Recursive
Definition: BMutex.h:16
int tryLock()
Test the lock.
Definition: BMutex.cpp:102
~BMutex()
Definition: BMutex.cpp:34
BMutex(Type type=Normal)
Definition: BMutex.cpp:12
int lock()
Set lock, wait as necessary.
Definition: BMutex.cpp:85
BMutex & operator=(const BMutex &mutex)
Definition: BMutex.cpp:30
int unlock()
Unlock the lock.
Definition: BMutex.cpp:98
int timedLock(int timeoutUs)
Set lock, wait as necessary but timeout after given time.
Definition: BMutex.cpp:89