Assuming
- no undefined behaviour occurs,
- no deadlocks occur,
- mutexes are locked and unlocked in the correct order by the correct threads the correct number of times,
- non-recursive mutexes are not locked multiple times,
- locking recursive mutexes does not exceed the maximum level of ownership,
- no predicates passed to condition variables throw, and
- only clocks, time points, and durations provided by the standard library are used with the
std::
mutexes and condition variables
is it guaranteed that operating on the different types of std::
mutexes and condition variables (other than on constructing them) does not throw any exceptions (especially of type std::system_error
)?
For example, in case of methods like:
void MyClass::setVariable() {
std::lock_guard<std::mutex> const guard(m_mutex);
m_var = 42; // m_var is of type int
m_conditionVariable.notify_all();
}
void MyClass::waitVariable() {
std::unique_lock<std::mutex> lock(m_mutex);
m_conditionVariable.wait(lock, [this]() noexcept { return m_var == 42; });
}
Is it safe to assume noexcept
or should one write some try-catch blocks around the callsites? Or are there any caveats?
Please consider all types of mutexes and condition variables in C++11, C++14 and later.
std::condition_variable::wait()
is changed tonoexcept
in C++14. It now just callsstd::terminate()
when reacquiring the lock fails. You might want to consider that. – Cucullate