zoukankan      html  css  js  c++  java
  • notify_one()产生的疑问

    #include <iostream>
    #include <thread>
    #include <mutex>
    #include <chrono>
    #include <condition_variable> 
     
    std::mutex mtx;
    std::condition_variable cv;
    bool ready = false;
    
    void print_id (int id) {
        std::unique_lock<std::mutex> lck(mtx);
        while (!ready) cv.wait(lck);
        std::cout << "thread " << id << '\n';
    }
     
    void go() {
        std::unique_lock<std::mutex> lck(mtx);
        ready = true;
        cv.notify_one();
    }
     
    int main() {
        std::thread threads[10];
    
        for (int i = 0; i < 10; ++i)
            threads[i] = std::thread(print_id, i);
        
        // std::this_thread::sleep_for(std::chrono::seconds(2));
    
        std::cout << "go().......!\n";
        go();
     
        for (auto& th : threads) th.join();
     
        return 0;
    }
    

    开了10个子线程,每个线程获得锁后,调用wait()释放锁并阻塞
    调用go()后获得互斥锁,调用notify_one()唤醒一个线程,被唤醒的线程输出线程编号

    但是以上代码执行结果

    go().......!
    thread 1
    thread 6
    thread 0
    thread 9
    thread 8
    

    相同代码再执行一次结果

    go().......!
    thread 0
    thread 3
    thread 5
    thread 4
    thread 6
    thread 2
    thread 7
    thread 9
    

    原因是因为主线程调用go()时,有些子线程还没有获得锁,即还没有调用wait()
    此时go()获得锁并设置ready = true,然后notify_one()唤醒一个线程再释放锁
    此时其他子线程获得锁,但是由于ready == true,所以跳过了wait()直接输出了线程编号
    在调用go()之前挂起主线程一定时间即可,即注释代码

  • 相关阅读:
    第十章 泛型程序设计与C++标准模板库 迭代器
    Linux 命令
    Linux 命令
    Linux 命令
    Linux 命令
    Linux 命令
    Linux 命令
    Linux 命令
    Linux 命令
    Linux 命令
  • 原文地址:https://www.cnblogs.com/Zeronera/p/15563518.html
Copyright © 2011-2022 走看看