Skip to content

Commit eb95419

Browse files
committed
CON50-CPP: Mutexes do not have destructors in most real libraries
Mark ~mutex() as deleted, as that is what we see in real libraries. Also modify lock_guard. This didn't have any affect on the test, but retained to ensure we better reflect real compilers.
1 parent e70089f commit eb95419

File tree

2 files changed

+7
-6
lines changed

2 files changed

+7
-6
lines changed

cpp/common/test/includes/standard-library/mutex.h

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ constexpr try_to_lock_t try_to_lock{};
2323
class mutex {
2424
public:
2525
constexpr mutex() noexcept;
26-
~mutex();
26+
~mutex() = default;
2727
mutex(const mutex &) = delete;
2828
mutex &operator=(const mutex &) = delete;
2929
void lock();
@@ -62,15 +62,19 @@ template <class Mutex>
6262
void swap(unique_lock<Mutex> &x, unique_lock<Mutex> &y) noexcept;
6363

6464
template <class _Lock0, class _Lock1, class... _LockN>
65-
void lock(_Lock0 &_Lk0, _Lock1 &_Lk1, _LockN &..._LkN) { }
65+
void lock(_Lock0 &_Lk0, _Lock1 &_Lk1, _LockN &..._LkN) {}
6666

6767
template <typename Mutex> class lock_guard {
6868
public:
6969
typedef Mutex mutex_type;
7070

71-
explicit lock_guard(mutex_type &__m);
71+
explicit lock_guard(mutex_type &__m) : _m(__m) { _m.lock(); }
7272
lock_guard(const lock_guard &) = delete;
7373
lock_guard &operator=(const lock_guard &) = delete;
74+
~lock_guard() { _m.unlock(); }
75+
76+
private:
77+
mutex_type &_m;
7478
};
7579

7680
} // namespace std
Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
| test.cpp:4:18:4:33 | call to mutex | Mutex used by thread potentially $@ while in use. | test.cpp:11:18:11:26 | call to ~mutex | destroyed |
21
| test.cpp:4:18:4:33 | call to mutex | Mutex used by thread potentially $@ while in use. | test.cpp:11:18:11:26 | delete | destroyed |
3-
| test.cpp:16:14:16:15 | call to mutex | Mutex used by thread potentially $@ while in use. | test.cpp:21:1:21:1 | call to ~mutex | destroyed |
42
| test.cpp:16:14:16:15 | call to mutex | Mutex used by thread potentially $@ while in use. | test.cpp:21:1:21:1 | return ... | destroyed |
5-
| test.cpp:94:8:94:23 | call to mutex | Mutex used by thread potentially $@ while in use. | test.cpp:10:34:10:42 | call to ~mutex | destroyed |
63
| test.cpp:94:8:94:23 | call to mutex | Mutex used by thread potentially $@ while in use. | test.cpp:10:34:10:42 | delete | destroyed |

0 commit comments

Comments
 (0)