简体   繁体   English

当调用condition_variable :: wait()时,未锁定的unique_lock会发生什么情况?

[英]what happen to an unlocked unique_lock when condition_variable::wait() is called?

Here's an sample code on a C++ reference website 这是C ++参考网站上的示例代码

#include <chrono>
#include <condition_variable>
#include <iostream>
#include <mutex>
#include <thread>

std::mutex mtx;
std::condition_variable cv;
bool ready = false;

void print_id(int id) {
    std::unique_lock<std::mutex> lck(mtx);
    while (!ready) {
        lck.unlock(); // by curiosity I unlock the lck
        cv.wait(lck);
    }
    std::cout << "thread " << id << '\n';
}

void go() {
    std::unique_lock<std::mutex> lck(mtx);
    ready = true;
    lck.unlock();
    cv.notify_all();
}

int main() {
    std::thread threads[10];
    for (int i = 0; i < 10; ++i)
        threads[i] = std::thread(print_id, i);
    go();
    for (auto &th : threads)
        th.join();

    return 0;
}

If I don't unlock the unique_lock before calling wait(), everything works normally. 如果在调用wait()之前未解锁unique_lock,则一切正常。 Like: 喜欢:

thread 9
thread 1
thread 2
thread 3
thread 4
thread 5
thread 6
thread 7
thread 8
thread 0

I've been told that 有人告诉我

At the moment of blocking the thread, the function( wait() ) automatically calls lck.unlock(), allowing other locked threads to continue. 在阻塞线程时,function( wait() )自动调用lck.unlock(),从而允许其他锁定的线程继续执行。

So I wonder what if I unlock unique_lock before wait() is called.After I did that, the program behaved weirdly, only one or two threads complete their job(print the "thread X" message), other threads seem to be blocked (by the unique_lock?) forever. 因此,我想知道是否在调用wait()之前解锁unique_lock。在执行此操作之后,程序表现异常,只有一个或两个线程完成其工作(打印“ thread X”消息),而其他线程似乎被阻塞了(唯一地锁定?)。

What happened to those unique_locks? 这些unique_locks发生了什么? Or it's just another undefined behavior of c++ that calling unlock() before wait()? 还是在wait()之前调用unlock()的c ++的另一种未定义行为?

Thanks! 谢谢!

As stated in documentation of std::condition_variable::wait() std :: condition_variable :: wait()的文档中所述

Calling this function if lock.mutex() is not locked by the current thread is undefined behavior. 如果lock.mutex()没有被当前线程锁定,则调用此函数是未定义的行为。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 尝试等待boost :: condition_Variable时出现“ unique_lock没有互斥量:不允许操作”错误 - “unique_lock has no mutex: Operation not permitted” error when attempting to wait on boost::condition_Variable 调用函数时,unique_lock是否已解锁? - Is unique_lock unlocked when a function is called? C ++ 11:为什么std :: condition_variable使用std :: unique_lock? - C++11: why does std::condition_variable use std::unique_lock? std :: unique_lock和std :: condition_variable如何工作 - How do std::unique_lock and std::condition_variable work condition_variable和unique_lock如何用于线程安全列表 - how condition_variable and unique_lock works for thread safe list 使用std :: mutex,std :: condition_variable和std :: unique_lock - Using std::mutex, std::condition_variable and std::unique_lock 将condition_variable与unique_lock一起使用会导致定期崩溃(GCC 4.7,OSX) - Using condition_variable with unique_lock causing periodic crash (GCC 4.7, OSX) std::condition_variable 在阻塞之前真的解锁给定的 unique_lock 对象吗? - Does std::condition_variable really unlock the given unique_lock object before blocking? 命令多个线程如何重新获取std :: unique_lock <std::mutex> 在std :: condition_variable :: notify_all之后 - Order how multiple threads reacquire std::unique_lock<std::mutex> after std::condition_variable::notify_all boost :: condition_variable等待没有锁定 - boost::condition_variable wait without a lock
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM