假設異常處理<mutex>和<condition_variable>
- 沒有發生不確定的行爲,
- 無死鎖發生,
- 互斥由正確的線程正確數量的鎖定和解鎖以正確的順序次,
- 非遞歸互斥鎖未鎖定多次,
- 鎖定遞歸互斥鎖不超過maximum level of ownership, 個
- 沒有謂詞傳遞給條件變量扔,並
- 只鐘錶,時間點和持續時間由標準庫提供與
std::
互斥和條件變量
是用它保證了操作上的不同類型的std::
互斥鎖和條件變量(除構造它們之外)不會拋出任何異常(尤其是類型std::system_error
)?
例如,在類似的方法情況:
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; });
}
它是安全的假設noexcept
還是應該寫一個各地callsites一些的try-catch塊?或者有任何警告?
請考慮C++ 11,C++ 14及更高版本中的所有類型的互斥鎖和條件變量。
你可以通過linux上的futex()實現來查找失敗的條件:https://github.com/torvalds/linux/blob/master/kernel/futex.c#L3147 – Arvid
'std :: condition_variable :: wait()'在C++中更改爲'noexcept' 14。當重新獲取鎖定失敗時,它現在只調用'std :: terminate()'。你可能想考慮一下。 – TFM
@TFM我不同意。你能引用一些文件嗎? –