吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 3494|回复: 7
收起左侧

[其他原创] 实现多线程安全的栈和队列

[复制链接]
UNICORNLI 发表于 2017-6-3 21:24
本帖最后由 UNICORNLI 于 2017-6-3 22:39 编辑

这一段研究了一下C++多线程,就拿基本的数据结构栈和队列来练手。
[C++] 纯文本查看 复制代码
//编译需要的头文件
#include <thread>
#include <mutex>
#include <stack>
#include <exception>
//待抛的empty_stack exception
struct empty_stack:std::exception
{
        const char* what() const throw();
};
//这里将stack的pop和top合并为一个pop操作,并且重载了pop操作;
template<typename T>
class threadsafe_stack
{
private:
        std::stack<T> data;
        mutable std::mutex m;
public:
        threadsafe_stack(){}
        threadsafe_stack(const threadsafe_stack& other)
        {        
                std::lock_guard<std::mutex> lock(m);
                data=other.data;
        }
        threadsafe_stack& operator=(const threadsafe_stack&) = delete;
        void push(T new_value)
        {
                std::lock_guard<std::mutex> lock(m);
                data.push_back(new_value);
        }
                
        std::shared_ptr<T> pop()
        {
                std::lock_guard<std::mutex> lock(m);
                if(data.empty())throw empty_stack();
                std::shared_ptr<T>const res(
                        std::make_shared<T>(data.top()));
                data.pop();
                return res;        
        }
        void pop(T& value)
        {
                std::lock_guard<std::mutex> lock(m);
                if(data.empty()) throw empty_stack();
                value = data.top();
                data.top();
        }
        bool empty()const
        {
                std::lock_guard<std::mutex> lock(m);
                return data.empty();        
        }
};

[C++] 纯文本查看 复制代码
#include <memory>
#include <condition_variable>
#include <mutex>
#include <queue>

template<typename T>
class threadsafe_queue
{
private:
        mutable std::mutex mut;
        std::queue<T> data_queue;
        std::condition_variable data_cond;
public:
        threadsafe_queue()
        {}
        threadsafe_queue(const threadsafe_queue& other)
        {
                std::lock_guard<std::mutex> lock(mut);
                data_queue = other.data_queue;
        }
        void push(T new_value)
        {
                std::lock_guard<std::mutex>lock(mut);
                data_queue.emplice_back(new_value);
                data_cond.notify_one();
        }
        void wait_and_pop(T& value)
        {
              std::unique_lock<std::mutex> lock(mut);
              data_cond.wait(lock,[this]{return !data_queue.empty();});
              value = data_queue.front();
              data_queue.pop();
        }
        std::shared_ptr<T> wait_and_pop()
        {        
            std::unique_lock<std::mutex> lock(mut);
            data_cond.wait(lock,[this]{return !data_queue.empty();});
            std::shared_ptr<T> res(
                std::make_shared<T>(std::move(data_queue.front())));
            data_queue.pop();
            return res;
        }
        bool try_pop(T& value)
        {
                std::lock_guard<std::mutex>lock(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>lock(mut);
                if(data_queue.empty())
                        return std::shared_ptr<T>();
                std::shared_ptr<T> res(
                   std::make_shared<T>(std::move(data_queue.front())));
                data_queue.pop();
                return res;
        }        

        bool empty() const
        {
                std::lock_guard<std::mutex> lock(mut);
                return data_queue.empty();
        }
};
        



发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

 楼主| UNICORNLI 发表于 2017-6-3 21:32
这是源文件

Code.zip

1.15 KB, 下载次数: 3, 下载积分: 吾爱币 -1 CB

code

夙杀々冷封 发表于 2017-6-4 10:14
 楼主| UNICORNLI 发表于 2017-6-4 10:33
2909094965 发表于 2017-6-4 11:09
这个妙不懂啊,晕晕晕,不解释
 楼主| UNICORNLI 发表于 2017-6-4 12:58
2909094965 发表于 2017-6-4 11:09
这个妙不懂啊,晕晕晕,不解释

我也刚刚开始研究,慢慢学,不追求速成
aqwly 发表于 2017-6-5 09:54
好东西,可以看看,先收藏了
萌鬼出没 发表于 2017-6-5 11:09
感谢分享
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止灌水或回复与主题无关内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-5-29 12:21

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表