名前空間
変種
操作

std::condition_variable_any::wait_until

提供: cppreference.com
 
 
スレッドサポートライブラリ
スレッド
(C++11)
(C++20)
(C++20)
this_thread 名前空間
(C++11)
(C++11)
(C++11)
相互排他
汎用ロック管理
(C++11)
(C++11)
(C++11)(C++11)(C++11)
(C++11)
(C++11)
条件変数
(C++11)
セマフォ
ラッチとバリア
(C++20)
(C++20)
フューチャー
(C++11)
(C++11)
(C++11)
 
 
template<class Lock, class Clock, class Duration >

std::cv_status
    wait_until( Lock& lock,

                conststd::chrono::time_point<Clock, Duration>& timeout_time );
(1) (C++11以上)
template<class Lock, class Clock, class Duration, class Pred >

bool wait_until( Lock& lock,
                 conststd::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,
                 conststd::chrono::time_point<Clock, Duration>& timeout_time,

                 Pred pred );
(3) (C++20以上)

条件変数が通知されるか指定された時点に達するか spurious wakeup が発生するまで現在のスレッドをブロックします。 オプションで何らかの述語が満たされるまでループします

1) アトミックに、 lock を解放し、現在実行中のスレッドをブロックし、 *this の待機中スレッドのリストに追加します。 notify_all() または notify_one() が実行されるか、絶対時点 timeout_time に達すると、スレッドのブロックは解除されます。 spurious にブロック解除される可能性もあります。 ブロックが解除されると、その理由にかかわらず、 lock が再取得され、 wait_until を抜けます。 例外によってこの関数を抜ける場合も、 lock は再取得されます。(C++14未満)
2) 以下と同等です。
while(!pred()){if(wait_until(lock, timeout_time)==std::cv_status::timeout){return pred();}}returntrue;
spurious wakeup を無視するためにこのオーバーロードを使用することができます。


3) 割り込み可能な待機。 指定された stoken の紐付いた停止状態に停止要求が行われた場合に通知されるように、 wait_until() の期間中 condition_variable_any を登録します。 以下と同等です。
while(!stoken.stop_requested()){if(pred())returntrue;if(wait_until(lock, timeout_time)==std::cv_status::timeout)return pred();}return pred();

これらの関数が事後条件 (lock が呼び出し元スレッドによってロックされている) を満たせない場合は、 std::terminate が呼ばれます。 例えば、ミューテックスの再取得で例外が投げられると、これが発生し得ます。

(C++14以上)

目次

[編集]引数

lock - BasicLockable の要件を満たす Lock 型のオブジェクト。 現在のスレッドによってロックされていなければなりません
stoken - 割り込みを登録する std::stop_token
timeout_time - 待機をやめる時点を表す std::chrono::time_point 型のオブジェクト
pred - 待機を続けるべき場合に ​false を返す述語。

述語関数のシグネチャは以下と同等であるべきです。

 bool pred();

[編集]戻り値

1)timeout_time によって指定された絶対タイムアウトに達した場合は std::cv_status::timeout、そうでなければ std::cv_status::no_timeout
2) 述語 pred がタイムアウト timeout_time 経過後も未だ false に評価される場合は false、そうでなければ true。 タイムアウトがすでに経過していれば、 pred の結果が評価されて返されます。
3)pred() (タイムアウトが満了したか停止が要求されたかにかかわらず)。

[編集]例外

1)

std::system_error を投げる場合があります。 lock.lock() または lock.unlock() によって投げられた例外を伝播する場合もあります。

実行中に clock、time_point、または duration によって投げられるあらゆる例外 (標準ライブラリによって提供される clock、time_point、および duration は、例外を投げることはありません)。

2)(1) と同様ですが、 pred によって投げられた例外を伝搬する場合もあります。
3)(2) と同じです。

[編集]ノート

timeout_time に紐付く時計が使用されます。 単調増加する時計であることは要求されません。 時計が不連続的に調節された場合、この関数の動作について何の保証もありません。 既存の実装は timeout_timeClock から 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>usingnamespace 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';elsestd::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

[編集]関連項目

条件変数が通知されるまで現在のスレッドをブロックします
(パブリックメンバ関数)[edit]
条件変数が通知されるか指定時間が経過するまで現在のスレッドをブロックします
(パブリックメンバ関数)[edit]
cnd_timedwaitC言語リファレンス
close