rspec/rules/S5999/cfamily/rule.adoc
2023-09-20 14:18:08 +02:00

45 lines
1.1 KiB
Plaintext

== Why is this an issue?
When constructing an ``++std::scoped_lock++``, the constructor arguments are used to list the mutexes that the ``++scoped_lock++`` will lock on the creation and unlock on destruction. It is possible to construct a ``++scoped_lock++`` without any parameter, but in that case, it does absolutely nothing and is just dead code, which was probably not the intent of the user.
=== Noncompliant code example
[source,cpp]
----
void f1(std::mutex &m) {
std::scoped_lock lock; // Noncompliant
// Do some work
}
----
=== Compliant solution
[source,cpp]
----
void f1(std::mutex &m) {
std::scoped_lock lock {m}; // Compliant
// Do some work
}
template<class... D>
void processAll(D &...data) {
scoped_lock lock {data.getMutex()...}; // Compliant, even if the list might be empty in some cases
// Do some work
}
----
== Resources
* {cpp} reference - https://en.cppreference.com/w/cpp/thread/scoped_lock[std::scoped_lock]
ifdef::env-github,rspecator-view[]
'''
== Comments And Links
(visible only on this page)
=== relates to: S5997
endif::env-github,rspecator-view[]