- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这是我的代码:
类
class carl{
public:
int x = 0;
std::mutex _mu;
std::condition_variable cond;
bool donecreating = false;
void createFood(){
if(x == 0){
std::unique_lock<mutex> locker(_mu);
x++;
std::cout<<"creating food.."<<std::endl;
cout<<"Food count: "<<x<<endl;
locker.unlock();
cond.notify_one(); //notify
std::this_thread::sleep_for(chrono::seconds(1)); //sleep
}
}
void eatFood(){
std::unique_lock<mutex> locker(_mu); //lock
std::cout<<"i am executing"<<std::endl; //notif
cond.wait(locker); //wait
x--; //process food
std::cout<<"eating food.."<<std::endl;
cout<<"Food left: "<<x<<endl;
locker.unlock();
}
};
函数又名线程一
void create(carl& carl){
for(int i=0;i>-100;i--){ //create 100 times
carl.createFood();
}
carl.donecreating = true; //done creating 100 food
}
主要
int main(int argc, char** argv) {
carl c; //init object
std::thread t1(create,std::ref(c)); //init thread
while(c.donecreating != true){ //exit condition is if the class is done creating food 100 times
c.eatFood();
}
t1.join();
return 0;
}
输出:
i am executing
creating food...
Food count: 1
eating food..
Food left: 0
我正在尝试跟踪我的代码,这是我目前的理解,我需要一些说明
1.) 编译时,主线程(消费者)比生产者线程快,因此它首先被启动,然后由 cond.wait(locker);
进入休眠状态以防止它进食,因为还没有制作食物。但在 cond.wait(locker);
之前有std::unique_lock<mutex> locker(_mu);
, 它是否自动解锁以便其他线程可以在等待时访问它?
2.) 如果有 createFood
首先启动(因为线程是基于处理器的?这可能吧?),它将发送 cond.notify_one();
如果有,如果没有,那么它只会继续创造一种食物,然后 sleep
.另一个线程将开始处理,因为 mutex
将被解锁然后它将达到cond.wait(locker);
但是已经有食物了sleep
没有必要,我发现的解决方案是实现 spurious wake
, 这就是它的用途吗?
3.) 我还是很好奇 std::unique_lock<mutex> locker(_mu);
就像如果另一个线程到达那行代码并且它当前被锁定会发生什么?它会忽略下面的每一行并继续前进直到该代码块超出范围吗?还是停在那条线上等待解锁?
最佳答案
首先,自 if(x == 0)
以来,您的代码存在数据竞争(因此存在未定义的行为)在createFood()
访问 x
不 protected 。每次访问 x
,读取和写入都必须受互斥锁保护。
is it automatically unlocked so that the other thread can access it while waiting?
是的,wait()
在阻塞条件变量之前解锁互斥锁,然后在它返回之前再次锁定互斥锁。
The other thread will start processing because the mutex will be unlocked then it will reach
cond.wait(locker);
but there is a food already so sleep won't be necessary the solution that i found out is by implementing spurious wake, is that what it is for?
不,虚假唤醒不是您可以控制的。您的代码有两个问题:
eatFood()
可以虚假结束,在这种情况下实际上没有任何食物可吃,但您也不会处理这种情况。解决方案是运行wait
循环,直到有食物可用。这相当于运行 wait
的版本取一个谓词:
while(x == 0) cond.wait(locker);
或
cond.wait(locker, [this](){ return x > 0; });
I'm still really curious about
std::unique_lock<mutex> locker(_mu);
like what really happens if the other thread reached that line of code and it's currently locked? does it ignore every line below that and just move on until that block of code gets out of scope? or does it stops on that line and wait until it gets unlocked?
后者。在该行之后,互斥体被构造locker
的线程锁定。 .这意味着在必要时进行阻塞和等待。
关于c++ - std::unique_lock<mutex> 和 conditional_variable cond 的关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30824578/
我在 Bjarne Stroustrup 的“C++ 编程语言,第 4 版”第 119 页中无意中发现了以下代码: queue mqueue; condition_variable mcond; mu
我不太清楚为什么 std::unique_lock比仅使用普通锁有用。我正在查看的代码示例是: {//aquire lock std::unique_lock lock(queue_m
请帮我看看我对 std::unique_lock 的理解移动语义是正确的。 假设我有一个函数 myFunc它需要在执行开始时获取互斥锁。 这个函数是从不同的函数调用的,其中一些函数例如myWrappe
我有一个应用程序,我使用 boost::shared_mutex 锁定在一个线程中编写一个 cv::Mat,然后从许多其他线程调用它。 这很好用,但是所有的读取都会减慢编写器的速度。我想这是因为写入正
Why would I use a unique_lock<> wrapper? 我有时会看到这样的代码 std::unique_lock lock(m_active_sessions_guard);
我遇到了一些关于 unique_lock 的奇怪行为。创建它之后,我尝试调用 unlock,但它使我的程序崩溃。我创建了一个最小的示例,该示例始终在解锁功能上崩溃(使用 gdb 进行确认)。 #inc
void Fn() { std::mutex* mutex = new std::mutex; std::unique_lock lock(*mutex); delete mutex; }
即使我手动调用了 unlock,unique_lock::unlock() 函数是否会在销毁时调用,以便在给定的互斥锁上接收到两次解锁信号? 最佳答案 不,它不会尝试解锁。如果你看unlock ,它
当开发人员在新范围内使用 std::unique_lock 自动解锁互斥量时,我看到了很多代码示例: ... // do some staff { std::unique_lock lock(sh
在使用类时,我试图更好地理解 std::mutex、std::lock_guard、std::unique_lock . 对于初学者来说,我知道 lock_guard 和 unique_lock 之间
我想在我的代码中使用线程,并认为即将到来的 C++0x 扩展会很方便,因为它们最终会成为一个标准。这似乎是面向 future 的,无需使用像 boost::thread 这样的额外库。 不幸的是,我找
我正在考虑使用以下代码: auto now = std::chrono::high_resolution_clock::now(); std::unique_lock lock(mutex, std:
我需要根据函数中的参数创建 unique_lock 或共享锁。我无法在功能范围内正确定义。所以我需要做这样的事情。 function (bool check) { lock; if (check)
在这个例子中,std::unique_lock 调用带有标志 std::defer_lock。在 cppreference 上写道:“defer_lock_t 不获取互斥体的所有权”和:“(析构函数)
我们使用的互斥锁有一个奇怪的接口(interface)(例如奇怪的外壳)。对 std::unique_lock 进行专门化将有助于减少使用此互斥锁的认知开销。 我在标准中找不到任何专门允许专门化 st
我正在使用 std::mutex 在函数的开头复制 std::shared_ptr。 // Lock the mutex unique_lock guard(d_mutex); // Copy a s
今天我在我们的代码中偶然发现了一个讨厌的错误: std::unique_lock (some_mutex); 注意缺少的标识符。我很惊讶这实际上编译并翻译成: std::unique_lock som
据我所知,std::unique_lock 是一种围绕底层 mutex 对象的包装器,以提供更安全使用原始 mutexes 实现(例如,如果抛出异常或销毁时,最终处于解锁状态)。这就是所有 std::
基于 C++ Equivalent to Java's BlockingQueue void push(T const& value) { // original version {
我在看this talk路易斯·布兰迪 (Louis Brandy),当一位观众提出这个显而易见的问题时: Why does std::unique_lock have a default const
我是一名优秀的程序员,十分优秀!