public member function
<condition_variable>
unconditional (1) | template <class Lock, class Clock, class Duration> cv_status wait_until (Lock& lck, const chrono::time_point<Clock,Duration>& abs_time); |
---|
predicate (2) | template <class Lock, class Clock, class Duration, class Predicate> bool wait_until (Lock& lck, const chrono::time_point<Clock,Duration>& abs_time, Predicate pred); |
---|
Wait until notified or time point
The execution of the current thread (which shall currently lock lck) is blocked either until notified or until abs_time, whichever happens first.
線上程阻塞時,函式會自動呼叫 lck.unlock()
,允許其他已鎖定的執行緒繼續執行。
Once notified or once it is abs_time, the function unblocks and calls lck.lock()
, leaving lck in the same state as when the function was called. Then the function returns (notice that this last mutex locking may block again the thread before returning).
Generally, the function is notified to wake up by a call in another thread either to member notify_one or to member notify_one. But certain implementations may produce spurious wake-up calls without any of these functions being called. Therefore, users of this function shall ensure their condition for resumption is met.
If pred is specified (2), the function only blocks if pred returns false
, and notifications can only unblock the thread when it becomes true
(which is especially useful to check against spurious wake-up calls). It behaves as if implemented as
1 2 3 4
|
while (!pred())
if ( wait_until(lck,abs_time) == cv_status::timeout)
return pred();
return true;
|
引數
- lck
- 當前被該執行緒鎖定的一個可鎖定物件。
所有對該物件的wait 成員函式的併發呼叫都必須使用相同的底層互斥鎖物件。
Lock 應為可鎖定型別。
- abs_time
- A point in time at which the thread will stop blocking, allowing the function to return.
time_point 是表示特定絕對時間的物件。
- pred
- 一個可呼叫物件或函式,它不接受任何引數,並返回一個可以被評估為
bool
的值。
此函式會反覆呼叫 pred,直到其求值為 true
。
返回值
The unconditional version (1) returns cv_status::timeout if the function returns because abs_time has been reached, or cv_status::no_timeout otherwise.
謂詞版本 (2) 返回 pred()
,而不管超時是否被觸發(儘管它只有在被觸發時才能為 false
)。
資料競爭
該函式執行三個原子操作:
- 對 lck 的初始解鎖和同時進入等待狀態。
- 解除等待狀態。
- 在返回前鎖定 lck。
對物件的原子操作按照一個單一的總順序進行排序,此函式中的三個原子操作以與上述相同的相對順序發生。
異常安全
如果任何引數的值對此函式無效(例如,如果 lck 的互斥鎖物件未被呼叫執行緒鎖定),則會導致未定義行為。
Otherwise, if an exception is thrown, both the condition_variable_any
object and the arguments are in a valid state (basic guarantee).
It may throw
system_error in case of failure (transmitting any error condition from the respective call to
lock or
unlock).
謂詞版本 (2) 也可能丟擲由
pred 丟擲的任何異常。
發生異常時,在退出函式作用域之前,會嘗試恢復
lck 的狀態(透過呼叫
lck.lock()
)。
如果與
abs_time 相關的操作丟擲異常,則此函式可能丟擲異常(請注意,在
<chrono>
中提供的標準
時鐘 和
持續時間 型別上的操作永遠不會丟擲)。
謂詞版本 (2) 也可能丟擲由
pred 丟擲的異常。
如果函式在某個時刻未能恢復鎖定並返回(例如,如果嘗試鎖定或解鎖時丟擲異常),則會呼叫
std::terminate。