std::condition_variable_any::wait_until
template< class Lock, class Clock, class Duration > std::cv_status wait_until( Lock& lock, const std::chrono::time_point<Clock, Duration>& timeout_time ); |
(1) | (C++11以上) |
template< class Lock, class Clock, class Duration, class Pred > bool wait_until( Lock& lock, const std::chrono::time_point<Clock, Duration>& timeout_time, Pred pred ); |
(2) | (C++11以上) |
template< class Lock, class Clock, class Duration, class Pred > void wait_until( Lock& lock, std::stop_token stoken, const std::chrono::time_point<Clock, Duration>& timeout_time, Pred pred ); |
(3) | (C++20以上) |
条件変数が通知されるか指定された時点に達するか spurious wakeup が発生するまで現在のスレッドをブロックします。 オプションで何らかの述語が満たされるまでループします
lock を解放し、現在実行中のスレッドをブロックし、 *this の待機中スレッドのリストに追加します。 notify_all() または notify_one() が実行されるか、絶対時点 timeout_time に達すると、スレッドのブロックは解除されます。 spurious にブロック解除される可能性もあります。 ブロックが解除されると、その理由にかかわらず、 lock が再取得され、 wait_until を抜けます。 例外によってこの関数を抜ける場合も、 lock は再取得されます。 (C++14未満)while (!pred()) {
if (wait_until(lock, timeout_time) == std::cv_status::timeout) {
return pred();
}
}
return true;
stoken の紐付いた停止状態に停止要求が行われた場合に通知されるように、 wait_until() の期間中 condition_variable_any を登録します。 以下と同等です。
while (!stoken.stop_requested()) {
if (pred())
return true;
if (wait_until(lock, timeout_time) == std::cv_status::timeout)
return pred();
}
return pred();
|
これらの関数が事後条件 ( |
(C++14以上) |
引数
| lock | - | BasicLockable の要件を満たす Lock 型のオブジェクト。 現在のスレッドによってロックされていなければなりません
|
| stoken | - | 割り込みを登録する std::stop_token
|
| timeout_time | - | 待機をやめる時点を表す std::chrono::time_point 型のオブジェクト |
| pred | - | 待機を続けるべき場合に false を返す述語。述語関数のシグネチャは以下と同等であるべきです。
|
戻り値
timeout_time によって指定された絶対タイムアウトに達した場合は std::cv_status::timeout、そうでなければ std::cv_status::no_timeout。pred がタイムアウト timeout_time 経過後も未だ false に評価される場合は false、そうでなければ true。 タイムアウトがすでに経過していれば、 pred の結果が評価されて返されます。pred() (タイムアウトが満了したか停止が要求されたかにかかわらず)。例外
|
std::system_error を投げる場合があります。 |
|
|
実行中に clock、time_point、または duration によって投げられるあらゆる例外 (標準ライブラリによって提供される clock、time_point、および duration は、例外を投げることはありません)。 |
pred によって投げられた例外を伝搬する場合もあります。ノート
timeout_time に紐付く時計が使用されます。 単調増加する時計であることは要求されません。 時計が不連続的に調節された場合、この関数の動作について何の保証もありません。 既存の実装は timeout_time を Clock から std::chrono::system_clock に変換し、 POSIX の pthread_cond_timedwait に移譲するため、待機はシステム時計の調節を反映しますが、ユーザ提供の Clock は反映しません。 いずれの場合でも、スケジューリングやリソースの奪い合いによる遅延のため、この関数は timeout_time に達するまでよりも長くブロックする可能性もあります。
使用される時計が std::chrono::steady_clock や他の単調な時計であっても、システム時計の調節によって spurious wakeup が誘発される可能性があります。
notify_one()/notify_all() および wait()/wait_for()/wait_until() の3つのアトミック部分 (unlock+wait、wakeup、lock) 各々の効果は、アトミック変数の変更順序として考えることができる単一の全順序で行われます。 この順序は個々の条件変数に固有です。 これにより、例えば notify_one() が遅延され、 notify_one() の呼び出しの後に待機を開始したスレッドをブロック解除することは、ありえなくなります。
例
#include <iostream>
#include <atomic>
#include <condition_variable>
#include <thread>
#include <chrono>
using namespace std::chrono_literals;
std::condition_variable cv;
std::mutex cv_m;
std::atomic<int> i{0};
void waits(int idx)
{
std::unique_lock<std::mutex> lk(cv_m);
auto now = std::chrono::system_clock::now();
if(cv.wait_until(lk, now + idx*100ms, [](){return i == 1;}))
std::cerr << "Thread " << idx << " finished waiting. i == " << i << '\n';
else
std::cerr << "Thread " << idx << " timed out. i == " << i << '\n';
}
void signals()
{
std::this_thread::sleep_for(120ms);
std::cerr << "Notifying...\n";
cv.notify_all();
std::this_thread::sleep_for(100ms);
i = 1;
std::cerr << "Notifying again...\n";
cv.notify_all();
}
int main()
{
std::thread t1(waits, 1), t2(waits, 2), t3(waits, 3), t4(signals);
t1.join();
t2.join();
t3.join();
t4.join();
}
出力例:
Thread 1 timed out. i == 0
Notifying...
Thread 2 timed out. i == 0
Notifying again...
Thread 3 finished waiting. i == 1
関連項目
| 条件変数が通知されるまで現在のスレッドをブロックします (パブリックメンバ関数) | |
| 条件変数が通知されるか指定時間が経過するまで現在のスレッドをブロックします (パブリックメンバ関数) | |
cnd_timedwait の C言語リファレンス
| |