我正在以下位置浏览条件变量文章
http://software.intel.com/en-us/blogs/2010/10/01/condition-variable-support-in-intel-threading-building-blocks/
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 有何不同?
感谢您帮助澄清我的问题。
I am going through condition variable article at following location
http://software.intel.com/en-us/blogs/2010/10/01/condition-variable-support-in-intel-threading-building-blocks/
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 );
}
}
My understanding is that we use condition variable to wait on an event. I have following questions
- Why are we using mutex here while we are using condition
variable?
- In consumer() function in while loop we are taking mutex and
waiting on condition, how can producer function can lock mutex
if consumer already taken it and how can it notify it doesn't it a deadlock?
- How unique_lock is different from scoped_lock?
Thanks for your help in clarfiying my questions.
发布评论
评论(4)
条件变量的基础知识需要锁才能正常工作。
只有具有锁的线程才应该尝试更改条件变量的状态(即通过调用条件变量函数之一(这也是为了保护您真正正在处理的对象))。
当您对条件变量调用wait()时,线程将进入睡眠状态并释放互斥锁。当线程被唤醒时,它必须在函数 wait() 返回到用户代码之前重新获取锁。
它不会死锁,因为 wait() 在使线程休眠之前释放锁。
在这种情况下没有。但如果您有任何具体的实现,请指定实现,我们可以更详细地讨论它。
The basics of the condition variable require a lock to work correctly.
Only the thread with the lock should be trying to change the state of the condition variable (ie by calling one of the condition variable functions (it is also to protect the object you are really working on)).
When you call wait() on the condition variable the thread is put to sleep and the mutex is released. When a thread is woken up it must re-acquire the lock before the function wait() returns to the user code.
It does not deadlock because wait() is releasing the lock before putting the thread to sleep.
In this context none. But if you have any specific implementation of these then please specify the implementation and we can discuss it in more detail.
它应该是(为了清楚起见):
来自: http://www.boost.org/doc/libs/1_46_0/doc/html/thread/synchronization.html#thread.synchronization.condvar_ref.condition_variable.wait
这非常相似。
It should be (for clarity):
From: http://www.boost.org/doc/libs/1_46_0/doc/html/thread/synchronization.html#thread.synchronization.condvar_ref.condition_variable.wait
which is very similar.
互斥体用于互斥(以便生产者和消费者在没有锁定的情况下不会改变全局状态),条件变量用于强加一些排序。
Mutex is for mutual exclusion (so that the producer & consumer don't alter global state without locking), condition variable is to impose some ordering.