• C++14读写锁demo-读写操作都在子线程中


    #include 
    #include 
    #include 
    #include 
    #include 
    
    using namespace std;
    
    shared_timed_mutex sLock;
    int data = 10;
    bool threadExitFlag = false;
    
    unsigned long getTime()
    {
        chrono::system_clock clock;
        int64_t s = chrono::duration_cast<chrono::seconds>(
                    clock.now().time_since_epoch()).count();
        return s;
    }
    
    void read_shared_data(int id)
    {
        while(!threadExitFlag){
                shared_lock<shared_timed_mutex> slk(sLock);
                cout << getTime() << " read thread " << id << " data=" << data << endl;
    
                this_thread::sleep_for(chrono::milliseconds(100));
        }
    }
    
    void addDataFunc(int id){
        while(!threadExitFlag){
            {
                unique_lock<shared_timed_mutex> ulk(sLock);        //写锁加锁
    
                cout << getTime() << " write thread "<< id << " begin,num:"<< ++data<<"-------------" << endl;
    
                if(data == 20){
                    threadExitFlag = true;
                }
    
            }
    
            this_thread::sleep_for(chrono::milliseconds(200));
        }
    }
    
    int main()
    {
        thread t1(read_shared_data, 1);                    //启动线程t1
        thread t2(read_shared_data, 2);                    //启动线程t2
        thread t3(addDataFunc,0);
    
        t1.join();
        t2.join();
        t3.join();
    
        return 0;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60

    输出为:

    1667375955 read thread 1 read data=10
    1667375955 read thread 2 read data=10
    1667375955 write thread 0 begin,num:11-------------
    1667375955 read thread 2 read data=11
    1667375955 read thread 1 read data=11
    16673759551667375955 read thread  read thread 2 read data=1 read data=1111
    
    1667375955 read thread 1 read data=11
    1667375955 read thread 2 read data=11
    1667375955 write thread 0 begin,num:12-------------
    1667375955 read thread 2 read data=12
    1667375955 read thread 1 read data=12
    1667375955 read thread 1 read data=12
    1667375955 read thread 2 read data=12
    1667375955 write thread 0 begin,num:13-------------
    1667375955 read thread 2 read data=13
    1667375955 read thread 1 read data=13
    16673759551667375955 read thread  read thread 21 read data=13 read data=13
    
    1667375955 write thread 0 begin,num:14-------------
    1667375955 read thread 2 read data=14
    1667375955 read thread 1 read data=14
    16673759551667375955 read thread  read thread 2 read data=1 read data=1414
    
    1667375956 write thread 0 begin,num:15-------------
    1667375956 read thread 2 read data=15
    1667375956 read thread 1 read data=15
    1667375956 read thread 1 read data=15
    1667375956 read thread 2 read data=15
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
  • 相关阅读:
    WEB3 在 React搭建的Dapp中通过redux全局获取并存储用户ETH与自定义token与交易所存储数量
    Java面向对象,全程无废话,偏实战
    【Kafka专题】Kafka快速实战以及基本原理详解
    Spring Cloud Gateway 中文文档
    Unity -- Animation(旧版动画组件)和Animator(新版动画器组件)
    副本机制在kafka中的实践
    openai chatGPT 原理通俗介绍
    【Python_PySide2学习笔记(十八)】勾选按钮QCheckBox类的基本用法
    List对象集合按照对象某一属性字段排序
    react-native android图标尺寸规范
  • 原文地址:https://blog.csdn.net/u012199908/article/details/127653417