Mutex or condition variable declared thread_local
ID |
c.correctness.thread_local_sync_object |
Severity |
low |
Remediation Complexity |
medium |
Remediation Risk |
medium |
Remediation Effort |
medium |
Resource |
Concurrency |
Language |
C / C++ |
Description
This synchronization object is declared thread_local, so each thread gets its own private instance. A mutex or condition variable only does anything useful when multiple threads share the same instance; a per-thread copy can never coordinate those threads with each other, so any lock or wait on it is a no-op with respect to the state it was meant to protect. Give the object ordinary (non-thread-local) storage duration so every thread contends on the same instance.
Rationale
This synchronization object is declared thread_local, so each thread gets its own private instance. A mutex or condition variable only does anything useful when multiple threads share the same instance; a per-thread copy can never coordinate those threads with each other, so any lock or wait on it is a no-op with respect to the state it was meant to protect. Give the object ordinary (non-thread-local) storage duration so every thread contends on the same instance.
The following code illustrates the pattern detected by this rule:
// FLAGGED: Mutex or condition variable declared thread_local
thread_local std::mutex tls_lock;