Pixel - 2021-08-18
#include <mutex>
#include <shared_mutex>

struct foobar
{
    int foo;
    std::shared_mutex foo_mtx;

    int bar;
    std::shared_mutex bar_mtx;
};

int main()
{
    foobar xyz;

    {
        std::shared_lock shared_foo_lock(xyz.foo_mtx, std::defer_lock);
        std::shared_lock shared_bar_lock(xyz.bar_mtx, std::defer_lock);
        std::scoped_lock shared_multi_lock(shared_foo_lock, shared_bar_lock);
     }
}

Severity: warning
Line: 20
Summary: CWE: 667
The lock is ineffective because the mutex is locked at the same scope as the mutex itself.

It looks like Cppcheck believes shared_foo_lock and shared_bar_lock are mutexes rather than (deferred) locks. shared_foo_lock and shared_bar_lock reference mutexes outside the same scope of the scoped_lock.

 

Last edit: Pixel 2021-08-18