c++消息队列的实现

摘要:
#ifndefNET_FRAME_CONCURRENT_QUEUE_H#defineNET_FRAME_CONCURRENT_QUEUE_H#include#include#includetemplate/*消息队列实现*/classConcurrentQueue{ConcurrentQueue&operat
#ifndef NET_FRAME_CONCURRENT_QUEUE_H  
#define NET_FRAME_CONCURRENT_QUEUE_H  
#include <queue>
#include <mutex>
#include <condition_variable>
template<class Type>  
/*消息队列实现*/  
classConcurrentQueue {  
        ConcurrentQueue& operator=(const ConcurrentQueue&) = delete;  
        ConcurrentQueue(const ConcurrentQueue& other) = delete;  
public:  
        ConcurrentQueue() : _queue(), _mutex(), _condition() { }  
virtual ~ConcurrentQueue() { }  
voidPush(Type record) {  
            std::lock_guard <std::mutex> lock(_mutex);  
            _queue.push(record);  
            _condition.notify_one();  
        }  
bool Pop(Type& record, bool isBlocked = true) {  
if(isBlocked) {  
                std::unique_lock <std::mutex> lock(_mutex);  
while(_queue.empty()) {  
                    _condition.wait(lock);  
                }  
            }  
else //If user wants to retrieve data in non-blocking mode  

            {  
                std::lock_guard <std::mutex> lock(_mutex);  
if(_queue.empty()) {  
return false;  
                }  
            }  
            record =std::move(_queue.front());  
            _queue.pop();  
return true;  
        }  
        int32_t Size() {  
            std::lock_guard <std::mutex> lock(_mutex);  
return_queue.size();  
        }  
boolEmpty() {  
            std::lock_guard <std::mutex> lock(_mutex);  
return_queue.empty();  
        }  
private:  
        std::queue <Type>_queue;  
mutable std::mutex _mutex;  
        std::condition_variable _condition;  
    };  
#endif //NET_FRAME_CONCURRENT_QUEUE_H  


(2)拥有消息队列的线程池的实现

.h文件如下

#ifndef NET_FRAME_THREAD_POOL_H  
#define NET_FRAME_THREAD_POOL_H  
#include "ConcurrentQueue.h"
#include <vector>
#include <queue>
#include <memory>
#include <thread>
#include <mutex>
#include <condition_variable>
#include <future>
#include <functional>
#include <stdexcept>  
#define MIN_THREADS 10  
    template<class Type>  
    classThreadPool {  
        ThreadPool& operator=(const ThreadPool&) = delete;  
        ThreadPool(const ThreadPool& other) = delete;  
    public:  
        ThreadPool(int32_t threads, std::function<void(Type& record)>handler);  
        virtual ~ThreadPool();  
        voidSubmit(Type record);  
    private:  
    private:  
        bool_shutdown;  
        int32_t _threads;  
        std::function<void(Type& record)>_handler;  
        std::vector <std::thread>_workers;  
        ConcurrentQueue <Type>_tasks;  
    };  
    template<class Type>
    ThreadPool<Type>::ThreadPool(int32_t threads, std::function<void(Type &record)>handler)  
            : _shutdown(false),  
              _threads(threads),  
              _handler(handler),  
              _workers(),  
              _tasks() {  
        if (_threads <MIN_THREADS)  
_threads =MIN_THREADS;  
        for (int32_t i = 0; i < _threads; ++i)  
            _workers.emplace_back(  
                    [this] {  
                        while (!_shutdown) {  
                            Type record;  
                            _tasks.Pop(record, true);  
                            _handler(record);  
                        }  
                    }  
            );  
    }  
    template<class Type>
    ThreadPool<Type>::~ThreadPool() {  
        for (std::thread &worker: _workers)  
            worker.join();  
    }  
    template<class Type>  
    void ThreadPool<Type>::Submit(Type record) {  
        _tasks.Push(record);  
    }  
#endif //NET_FRAME_THREAD_POOL_H  

免责声明:文章转载自《c++消息队列的实现》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇使用JAVA调用KRPANO加密XML双色球历史数据下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

Qt编译mysql以及创建表后进行导入操作

       鉴于很多同学对Qt编译myql总是不能成功。出现各种问题,今天特此写出本教程,希望可以帮到须要的同学。        首先,须要明确编译的目的和原理。       目的:Qt 5.2版本号曾经都是不带mysql驱动的。所以须要进行编译mysql数据库驱动,仅仅有编译完毕后才干被Qt载入上。假设你安装的是Qt5.2以后版本号的,那就不须要了,...

RabbitMQ在普通MAVEN项目中的使用

五、在普通的Maven应用中使用MQ rabbitmq的队列结构 5.1简单模式 5.1.1 消息生产者 创建Maven项目 添加RabbitMQ连接所需要的依赖 <!--https://mvnrepository.com/artifact/com.rabbitmq/amqp-client --> <dependency>...

libuv::进程

对于基于事件(event-based)的程序来说, 有个限制,没办法很好地利用多核,提高CPU使用率. 即使能够使用多线程编程来分发 handle, 但是每个 loop 还是只有一个线程. 这时候, 使用多进程就能够分担 loop 的压力,并且通过多进程 + 通信的方法, 会比 多线程 + 共享内存的方法更加安全, 易于开发. #include <c...

RabbitMQ(二)核心组件介绍

前言 本文主要介绍AMQP核心组件: RabbitAdmin SpringAMQP 声明 RabbitTemplate SimpleMessageListenerContainer MessageListenerAdapte MessageConverter RabbitAdminRabbitAdmin类是针对RabbitMQ管理端进行封装操作,比如:Ex...

Jsp的九大对象,七大动作,三大指令

jsp九大内置对象:1>out 向客户端输出数据,字节流.如out.print(" dgaweyr"); 2>request 接收客户端的http请求.String getParameter(String name):得到表单参数名name的值.String[] getParameterValues(String name):(得到String...

qt 界面去掉系统边框

该代码在Qt5框架编辑,使用该类时, 直接继承这个类就可以了。 实现了拖拽功能和关闭功能,如果需要放大缩小功能, 需自己实现。 1 #ifndef CUSTOMIZE_QWIDGET_H 2 #define CUSTOMIZE_QWIDGET_H 3 #include <QWidget> 4 #include <QMouseE...