简体   繁体   English

unique_lock.lock() 导致 C++ 中止

[英]unique_lock.lock() causes abort in C++

I am learning C++ threads and i don't understand unique_lock mechanism very well.我正在学习 C++ 个线程,我不太了解 unique_lock 机制。 I reed This Link with Conditional variable , and more examples here but still I have my confusions:我阅读了带有条件变量的链接, 这里还有更多示例,但我仍然感到困惑:

1- So my question clearly is, doesn't unique_lock protect the mutual exclusion? 1- 所以我的问题显然是,unique_lock 不保护互斥吗? I see in some examples when we use it on a shared mutex, the second thread cannot enter to that area which what I expect.我在一些示例中看到,当我们在共享互斥锁上使用它时,第二个线程无法进入我期望的那个区域。 But in this example as you see the output, all the threads can pass this line: std::unique_lockstd::mutex lck(mtx);但是在这个例子中,正如您看到的 output,所有线程都可以通过这一行: std::unique_lockstd::mutex lck(mtx); is it just declaration or mutex gets locked as it declared?它只是声明还是互斥锁在声明时被锁定?

2- why does the.lock() cause abort error? 2- 为什么 the.lock() 会导致中止错误? If I comment out that line all the threads starts in a row as you see in the screen shot output. I expect only thread0 pass the std::unique_lock<std::mutex> lck(mtx);如果我注释掉该行,所有线程都会连续开始,如屏幕截图 output 中所示。我希望只有 thread0 通过std::unique_lock<std::mutex> lck(mtx); it should be locked for other threads它应该被其他线程锁定

在此处输入图像描述

Thanks谢谢

#include <mutex>
using namespace std;

condition_variable cv;
bool ready = false;
mutex mtx;
void print_id(int id) {

    // why all the threads can pass this line?
    std::unique_lock<std::mutex> lck(mtx);
    
   //i knew about the concept of two times locking, just thought there 
   //is something wrong with the constructor or i dont understand
    lck.lock(); // Having this line gives me abort.

    std::cout << "thread Starts: " << id << '\n';
    while (!ready) 
        cv.wait(lck);
    // ...
    std::cout << "thread Ends: " << id << '\n';
}

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

void main()
{
    std::thread threads[5];
    // spawn 10 threads:
    for (int i = 0; i < 5; ++i)
    {
        this_thread::sleep_for(chrono::milliseconds(2000));
        threads[i] = std::thread(print_id, i);
    }

    std::cout << "10 threads ready to race...\n";
    go();                       // go!

    for (auto& th : threads) th.join();
}

std::unique_lock is an RAII type . std::unique_lockRAII 类型 When an object of that type is constructed, it locks the mutex that was passed to it, and upon destruction it unlocks the mutex, so you have scope level locking and unlocking.当构建该类型的 object 时,它会锁定传递给它的互斥量,并在销毁时解锁互斥量,因此您有 scope 级别的锁定和解锁。

All that means is that when you do lck.lock();这意味着当你做lck.lock(); you are trying to lock a mutex you have already locked by creating lck .您正在尝试通过创建lck来锁定您已经锁定的互斥锁。 std::unique_lock::lock() will throw an exception when you do this, and it is that uncaught exception that is causing abort() to be called.执行此操作时, std::unique_lock::lock()将抛出异常,而正是该未捕获的异常导致调用abort()

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM