condition_variable_any used with a plain std::mutex

ID

c.correctness.condition_variable_any_with_mutex

Severity

low

Remediation Complexity

trivial

Remediation Risk

low

Remediation Effort

low

Resource

Concurrency

Language

C / C++

Description

This std::condition_variable_any waits on a std::unique_lock<std::mutex>. The _any variant exists to work with lock types other than std::mutex (a shared lock, a custom BasicLockable), and that generality costs it a heavier, type-erased implementation than std::condition_variable. When the lock is a plain std::mutex, use std::condition_variable instead — it does the same job with less overhead.

Rationale

This std::condition_variable_any waits on a std::unique_lock<std::mutex>. The _any variant exists to work with lock types other than std::mutex (a shared lock, a custom BasicLockable), and that generality costs it a heavier, type-erased implementation than std::condition_variable. When the lock is a plain std::mutex, use std::condition_variable instead — it does the same job with less overhead.

The following code illustrates the pattern detected by this rule:

std::mutex m;
std::condition_variable_any cv_any;
bool ready = false;
void waits_on_plain_mutex()
{
    std::unique_lock<std::mutex> lock(m);
    // FLAGGED: condition_variable_any used with a plain std::mutex
    cv_any.wait(lock, [] { return ready; });
}

Remediation

Follow secure coding practices and review the references below for detailed remediation guidance.

Configuration

This detector does not need any configuration.

© 2026 Xygeni. All rights reserved