zoukankan      html  css  js  c++  java
  • 多线程c++11编程题目

    /*题目:子线程循环 10 次,接着主线程循环 100 次,接着又回到子线程循环 10 次,接着再回到主线程又循环 100 次。          
    如此循环50次,试写出代码。子线程与主线程必有一个满足条件(flag == num),
    不满足条件的那个线程不可能获取unique_lock(会在wait中释放),只有满足条件的线程才能获取锁,执行程序
    */
    
    #include<thread>
    #include<iostream>
    #include<mutex>
    #include<condition_variable>
    
    using namespace std;
    
    mutex m;//保护条件的互斥访问
    condition_variable cond;//条件变量
    int flag = 10;//全局变量的值对线程来说是共享的 
    
    void fun(int num) 
    {
        for (int i = 0; i<50; i++) 
        {
            cout<<"num111111111111 = "<<num<<endl;
            unique_lock<mutex> lk(m);//A unique lock is an object that manages a mutex object with unique ownership in both states: locked and unlocked.  
            cout<<"num222222222222 = "<<num<<endl;
            //while(1)
            while (flag != num)//线程1 显然开始不进入 while  wait函数前面切记使用while 防止惊群现象
                cond.wait(lk);//在调用wait时会执行 lk.unlock()  哪个线程调用wait 哪个线程就释放mutex  因为这里的锁也是需要竞争的
            for (int j = 0; j<num; j++)
                cout << j << " ";
            cout << endl;
            flag = (num == 10) ? 100 : 10;
            cond.notify_one();//被阻塞的线程唤醒后lk.lock()恢复在调用wait前的状态  
        }
    }
    
    int main() {
        thread child(fun, 10);
        fun(100);
        child.join();
        while(1);
        return 0;
    }

    上面那样很容易理解 

    去掉注释的while(1)

    结果是:

    /learing/5-6#  ./thread1
    num111111111111100
    num222222222222100
    num11111111111110

    卡在这里了   自己理解吧!  或看注释!

    https://www.cnblogs.com/ljygoodgoodstudydaydayup/p/5950400.html

     https://www.cnblogs.com/waterfall/p/7994384.html

  • 相关阅读:
    Hdu 5396 Expression (区间Dp)
    Lightoj 1174
    codeforces 570 D. Tree Requests (dfs)
    codeforces 570 E. Pig and Palindromes (DP)
    Hdu 5385 The path
    Hdu 5384 Danganronpa (AC自动机模板)
    Hdu 5372 Segment Game (树状数组)
    Hdu 5379 Mahjong tree (dfs + 组合数)
    Hdu 5371 Hotaru's problem (manacher+枚举)
    Face The Right Way---hdu3276(开关问题)
  • 原文地址:https://www.cnblogs.com/zhangkele/p/10817383.html
Copyright © 2011-2022 走看看