次の場所で条件変数の記事を読んでいます
Here we have following code as example
#include "tbb/compat/condition_variable"
using namespace std;
condition_variable my_condition;
tbb::mutex my_mtx;
bool present = false;
void producer() {
unique_lock<tbb::mutex> ul( my_mtx );
present = true;
my_condition.notify_one();
}
void consumer() {
while( !present ) {
unique_lock<tbb::mutex> ul( my_mtx );
my_condition.wait( ul );
}
}
私の理解では、条件変数を使用してイベントを待機しています。次の質問があります
- 条件変数を使用しているのに、なぜここでミューテックスを使用しているのですか?
- whileループのconsumer()関数で、ミューテックスを取得して条件を待機しています。コンシューマーがすでにミューテックスを取得している場合、プロデューサー関数はどのようにミューテックスをロックでき、デッドロックではないことを通知できますか?
- unique_lockはscoped_lockとどのように異なりますか?
私の質問を明確にするためにあなたの助けをありがとう。