BDS Public
Beamlib 3.3.4
This is the Beam C++ class library.
Loading...
Searching...
No Matches
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
14class BMutex {
15public:
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);
28private:
29 pthread_mutex_t omutex;
30};
31
34public:
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(); }
39private:
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
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