1 | // |
2 | // ScopedUnlock.h |
3 | // |
4 | // Library: Foundation |
5 | // Package: Threading |
6 | // Module: Mutex |
7 | // |
8 | // Definition of the ScopedUnlock template class. |
9 | // |
10 | // Copyright (c) 2007, Applied Informatics Software Engineering GmbH. |
11 | // and Contributors. |
12 | // |
13 | // SPDX-License-Identifier: BSL-1.0 |
14 | // |
15 | |
16 | |
17 | #ifndef Foundation_ScopedUnlock_INCLUDED |
18 | #define Foundation_ScopedUnlock_INCLUDED |
19 | |
20 | |
21 | #include "Poco/Foundation.h" |
22 | |
23 | |
24 | namespace Poco { |
25 | |
26 | |
27 | template <class M> |
28 | class ScopedUnlock |
29 | /// A class that simplifies thread synchronization |
30 | /// with a mutex. |
31 | /// The constructor accepts a Mutex and unlocks it. |
32 | /// The destructor locks the mutex. |
33 | { |
34 | public: |
35 | inline ScopedUnlock(M& mutex, bool unlockNow = true): _mutex(mutex) |
36 | { |
37 | if (unlockNow) |
38 | _mutex.unlock(); |
39 | } |
40 | inline ~ScopedUnlock() |
41 | { |
42 | try |
43 | { |
44 | _mutex.lock(); |
45 | } |
46 | catch (...) |
47 | { |
48 | poco_unexpected(); |
49 | } |
50 | } |
51 | |
52 | private: |
53 | M& _mutex; |
54 | |
55 | ScopedUnlock(); |
56 | ScopedUnlock(const ScopedUnlock&); |
57 | ScopedUnlock& operator = (const ScopedUnlock&); |
58 | }; |
59 | |
60 | |
61 | } // namespace Poco |
62 | |
63 | |
64 | #endif // Foundation_ScopedUnlock_INCLUDED |
65 | |