-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathauto_mutex.cpp
51 lines (40 loc) · 904 Bytes
/
auto_mutex.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
//
// auto_mutex.cpp - auto mutex implementation
//
// leccore library, part of the liblec library
// Copyright (c) 2019 Alec Musasa (alecmus at live dot com)
//
// Released under the MIT license. For full details see the
// file LICENSE.txt
//
#include "leccore_common.h"
#include <mutex>
class mutex;
// Wrapper for the std::mutex object.
class liblec::leccore::mutex::impl {
std::mutex _mtx;
public:
impl() {}
~impl() {}
void lock() {
_mtx.lock();
}
void unlock() {
_mtx.unlock();
}
};
liblec::leccore::mutex::mutex() : _d(*new impl()) {}
liblec::leccore::mutex::~mutex() { delete& _d; }
class liblec::leccore::auto_mutex::impl {
mutex& _mtx;
public:
impl(mutex& mtx) :
_mtx(mtx) {
_mtx._d.lock();
}
~impl() {
_mtx._d.unlock();
}
};
liblec::leccore::auto_mutex::auto_mutex(mutex& mtx) : _d(*new impl(mtx)) {}
liblec::leccore::auto_mutex::~auto_mutex() { delete& _d; }