std::condition_variable
提供: cppreference.com
![]() |
このページは、Google 翻訳を使って英語版から機械翻訳されました。
翻訳には誤りや奇妙な言い回しがあるかもしれません。文章の上にポインタをおくと、元の文章が見れます。誤りを修正して翻訳を改善する手助けをしてください。翻訳についての説明は、ここをクリックしてください。 |
Defined in header <condition_variable>
|
||
class condition_variable; |
(C++11およびそれ以降) | |
:
condition_variable
クラスがするまで、同時にスレッド、または複数のスレッドをブロックするために使用することができる同期プリミティブですOriginal:
The
condition_variable
class is a synchronization primitive that can be used to block a thread, or multiple threads at the same time, until:The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
- 通知は、別のスレッドから受信されますOriginal:a notification is received from another threadThe text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions. - タイムアウトが満了した場合、Original:a timeout expires, orThe text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions. - スプリアスウェイクアップが発生しますOriginal:a スプリアスウェイクアップ occursThe text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
std::condition_variable
で待機しようとするスレッドは、最初std::unique_lockを取得する必要があります。待機操作がアトミックにmutexを解放し、スレッドの実行を中断します。条件変数が通知されると、スレッドが起こされ、mutexは再取得される.Original:
Any thread that intends to wait on
std::condition_variable
has to acquire a std::unique_lock first. The wait operations atomically release the mutex and suspend the execution of the thread. When the condition variable is notified, the thread is awakened, and the mutex is reacquired.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
クラス
std::condition_variable
はStandardLayoutType
です。そうではありませんCopyConstructible
、MoveConstructible
、CopyAssignable
、MoveAssignable
.Original:
The class
std::condition_variable
is a StandardLayoutType
. It is not CopyConstructible
, MoveConstructible
, CopyAssignable
, MoveAssignable
.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
[編集] メンバータイプ
メンバー·タイプ
Original: Member type The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. |
Definition |
native_handle_type
|
実装定義
Original: implementation-defined The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. |
[編集] メンバ関数
オブジェクトを作成します Original: constructs the object The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (パブリックメンバ関数) | |
オブジェクトを破棄します Original: destructs the object The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (パブリックメンバ関数) | |
operator= [削除された] |
コピーではなくアサイン可能 Original: not copy-assignable The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (パブリックメンバ関数) |
Original: Notification The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. | |
1待機中のスレッドに通知します Original: notifies one waiting thread The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (パブリックメンバ関数) | |
待機中のすべてのスレッドに通知します Original: notifies all waiting threads The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (パブリックメンバ関数) | |
Original: Waiting The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. | |
条件変数はウェイクアップされるまで、現在のスレッドをブロックします Original: blocks the current thread until the condition variable is woken up The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (パブリックメンバ関数) | |
現在のスレッドのブロックを条件変数が目覚めたり、指定されたタイムアウト期間の後にされるまで Original: blocks the current thread until the condition variable is woken up or after the specified timeout duration The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (パブリックメンバ関数) | |
条件変数はウェイクアップされるまで、または指定された時点に達するまで、現在のスレッドをブロックします Original: blocks the current thread until the condition variable is woken up or until specified time point has been reached The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (パブリックメンバ関数) | |
Original: Native handle The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. | |
ネイティブハンドルを返します Original: returns the native handle The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (パブリックメンバ関数) |
[編集] 例
このコードを実行します
#include <condition_variable> #include <mutex> #include <thread> #include <iostream> #include <queue> #include <chrono> int main() { std::queue<int> produced_nums; std::mutex m; std::condition_variable cond_var; bool done = false; bool notified = false; std::thread producer([&]() { for (int i = 0; i < 5; ++i) { std::this_thread::sleep_for(std::chrono::seconds(1)); std::unique_lock<std::mutex> lock(m); std::cout << "producing " << i << '\n'; produced_nums.push(i); notified = true; cond_var.notify_one(); } done = true; cond_var.notify_one(); }); std::thread consumer([&]() { std::unique_lock<std::mutex> lock(m); while (!done) { while (!notified) { // loop to avoid spurious wakeups cond_var.wait(lock); } while (!produced_nums.empty()) { std::cout << "consuming " << produced_nums.front() << '\n'; produced_nums.pop(); } notified = false; } }); producer.join(); consumer.join(); }
Possible output:
producing 0 consuming 0 producing 1 consuming 1 producing 2 consuming 2 producing 3 consuming 3 producing 4 consuming 4