Multiple threads waiting on same condition variable
Using cnd_signal
or
std::condition_variable::notify_one()
to notify one of the threads
waiting for the same condition variable might result in indefinite blocking
Since R2020a
Description
This checker is deactivated in a default Polyspace® as You Code analysis. See Checkers Deactivated in Polyspace as You Code Analysis (Polyspace Access).
This defect occurs when you use cnd_signal
family functions or the
std::condition_variable::notify_one()
function to notify one of at least
two threads that are concurrently waiting for the same condition variable. For threads with
the same priority level, these functions cause the thread scheduler to arbitrarily notify one
of the threads waiting on the condition variable.
Polyspace reports this check on the function call. See the Event column in the Results Details pane to view the threads waiting on the same condition variable.
Risk
When multiple threads use the same condition variable, then the
cnd_signal
family function or the
std::condition_variable::notify_one()
function arbitrarily notifies one
of the waiting threads. The notified thread usually tests for a condition predicate. If the
condition predicate is false, the thread continues to wait until it is notified again.
Because this method notifies an arbitrary thread, it is possible that the condition
predicate for the notified thread never becomes true
. The program ends up
in a state where no thread is awake to signal the condition variable, resulting in
indefinite blocking.
Fix
Use cnd_broadcast
family functions or
std::condition_variable::notify_all()
to notify all threads waiting on
the condition variable, or use a unique condition variable for each thread.
Examples
Result Information
Group: Concurrency |
Language: C | C++ |
Default: Off |
Command-Line Syntax:
SIGNALED_COND_VAR_NOT_UNIQUE |
Impact: Low |
Version History
Introduced in R2020a
See Also
Find defects
(-checkers)
| Function that can spuriously fail not
wrapped in loop
| Function that can spuriously wake up
not wrapped in loop
| Data
race
| Missing
unlock
| Missing
lock
| Deadlock
| Multiple mutexes with one conditional
variable
Topics
- Interpret Bug Finder Results in Polyspace Desktop User Interface
- Interpret Bug Finder Results in Polyspace Access Web Interface (Polyspace Access)
- Address Results in Polyspace User Interface Through Bug Fixes or Justifications
- Address Results in Polyspace Access Through Bug Fixes or Justifications (Polyspace Access)