guard.hpp
1.65 KB
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// Copyright (C) 2000 Stephen Cleary
//
// Distributed under the Boost Software License, Version 1.0. (See
// accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
//
// See http://www.boost.org for updates, documentation, and revision history.
#ifndef BOOST_POOL_GUARD_HPP
#define BOOST_POOL_GUARD_HPP
/*!
\file
\brief Extremely Light-Weight guard class.
\details Auto-lock/unlock-er
detail/guard.hpp provides a type guard<Mutex>
that allows scoped access to the Mutex's locking and unlocking operations.
It is used to ensure that a Mutex is unlocked, even if an exception is thrown.
*/
namespace boost {
namespace details {
namespace pool {
template <typename Mutex> //!< \tparam Mutex (platform-specific) mutex class.
class guard
{ //! Locks the mutex, binding guard<Mutex> to Mutex.
/*! Example:
Given a (platform-specific) mutex class, we can wrap code as follows:
extern mutex global_lock;
static void f()
{
boost::details::pool::guard<mutex> g(global_lock);
// g's constructor locks "global_lock"
... // do anything:
// throw exceptions
// return
// or just fall through
} // g's destructor unlocks "global_lock"
*/
private:
Mutex & mtx;
guard(const guard &); //!< Guards the mutex, ensuring unlocked on destruction, even if exception is thrown.
void operator=(const guard &);
public:
explicit guard(Mutex & nmtx)
:mtx(nmtx)
{ //! Locks the mutex of the guard class.
mtx.lock();
}
~guard()
{ //! destructor unlocks the mutex of the guard class.
mtx.unlock();
}
}; // class guard
} // namespace pool
} // namespace details
} // namespace boost
#endif