《C++并发编程实战》读书笔记(1)

这两天开始看《C++并发编程实战》一书,感觉受益匪浅啊!

按照书中的同步并发操作(第四章)的内容,尝试编写线程安全的队列,模拟固定采集时间和不确定处理时间下的队列行为,供大家参考。

用到的C++多线程相关的主要内容为:mutex类(锁对象),lock_guard模板(实现锁对象的RAII用法),unique_lock模版,condition_variable类(用于进程间等待与唤醒)

另外,书中提供了非常好的技巧,如mutex对象在类中设置为mutable(可变),以用于静态成员函数等。

上代码,线程安全队列部分参考书上写的,仅供大家学习交流。


这个是threadsafe_queue的头文件:

#ifndef SQUEUE
#define SQUEUE

#include <queue>
#include <mutex>
#include <condition_variable>
#include <memory>

template<typename T>
class threadsafe_queue{
private:
    mutable std::mutex mut;
    std::queue<T> data_queue;
    std::condition_variable data_cond;
public:
    threadsafe_queue() = default;
    threadsafe_queue(const threadsafe_queue &other){
        std::lock_guard<std::mutex> lk(other.mut);
        data_queue = other.data_queue;
    }
    threadsafe_queue &operator=(const threadsafe_queue &other){
        std::lock_guard<std::mutex> lk(other.mut);
        data_queue = other.data_queue;
    }
    void push(T new_val){
        std::lock_guard<std::mutex> lk(mut);
        data_queue.push(new_val);
        data_cond.notify_all();
    }
    void wait_and_pop(T &value){
        std::unique_lock<std::mutex> lk(mut);
        data_cond.wait(lk, [this](){return !data_queue.empty(); });
        value = data_queue.front();
        data_queue.pop();
    }
    std::shared_ptr<T> wait_and_pop(){
        std::unique_lock<std::mutex> lk(mut);
        data_cond.wait(lk, [this](){return !data_queue.empty()});
        std::shared_ptr<T> ptr(std::make_shared<T>(data_queue.front()));
        data_queue.pop();
        return ptr;
    }
    bool try_pop(T &value){
        std::lock_guard<std::mutex> lk(mut);
        if (data_queue.empty())
            return false;
        value = data_queue.front();
        data_queue.pop();
        return true;
    }
    std::shared_ptr<T> try_pop(){
        std::lock_guard<std::mutex> lk(mut);
        if (data_queue.empty())
            return std::shared_ptr<T>();
        std::shared_ptr<T> ptr(std::make_shared<T>(data_queue.front()));
        data_queue.pop();
        return ptr;
    }
    bool empty() const{
        std::lock_guard<std::mutex> lk(mut);
        return data_queue.empty();
    }
    int size() const{
        std::lock_guard<std::mutex> lk(mut);
        return data_queue.size();
    }
};

#endif


测试代码:

#include "S_queue.h"
#include <vector>
#include <iostream>
#include <thread>
#include <string>
#include <random>
#include <condition_variable>

using namespace std;

void fun1(int value, threadsafe_queue<int> &iq){
    uniform_int_distribution<int> u(-100, 100);
    default_random_engine e(time(0));
    for (int i = 0; i != value; --i){
        this_thread::sleep_for(chrono::milliseconds(100));
        iq.push(u(e));
    }
}

void fun2(int value, threadsafe_queue<int> &iq){
    uniform_int_distribution<int> u(90, 105);
    default_random_engine e(time(0));
    for (int i = 0; i != value; --i){
        this_thread::sleep_for(chrono::milliseconds(u(e)));
        int val;
        cout << "Size: " << iq.size() << " —— ";
        iq.wait_and_pop(val);
        cout << "Value: " << val << endl;
    }
}

int _tmain(int argc, _TCHAR* argv[])
{
    string line;
    threadsafe_queue<int> iq;
    int num;
    cin >> num;
    thread t1(fun1, num, ref(iq)), t2(fun2, num, ref(iq));
    t1.join();
    t2.detach();

    cin >> line;
    return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值