c++消息队列的实现

(1)消息队列的实现

 

#ifndef NET_FRAME_CONCURRENT_QUEUE_H
#define NET_FRAME_CONCURRENT_QUEUE_H

#include 
#include 
#include 



    template
	/*消息队列实现*/
    class ConcurrentQueue {
        ConcurrentQueue& operator=(const ConcurrentQueue&) = delete;

        ConcurrentQueue(const ConcurrentQueue& other) = delete;

    public:
        ConcurrentQueue() : _queue(), _mutex(), _condition() { }

        virtual ~ConcurrentQueue() { }

        void Push(Type record) {
            std::lock_guard  lock(_mutex);
            _queue.push(record);
            _condition.notify_one();
        }

        bool Pop(Type& record, bool isBlocked = true) {
            if (isBlocked) {
                std::unique_lock  lock(_mutex);
                while (_queue.empty()) {
                    _condition.wait(lock);
                }
            }
            else // If user wants to retrieve data in non-blocking mode
            {
                std::lock_guard  lock(_mutex);
                if (_queue.empty()) {
                    return false;
                }
            }

            record = std::move(_queue.front());
            _queue.pop();
            return true;
        }

        int32_t Size() {
            std::lock_guard  lock(_mutex);
            return _queue.size();
        }

        bool Empty() {
            std::lock_guard  lock(_mutex);
            return _queue.empty();
        }

    private:
        std::queue  _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 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

#define MIN_THREADS 10


    template
    class ThreadPool {
        ThreadPool& operator=(const ThreadPool&) = delete;

        ThreadPool(const ThreadPool& other) = delete;

    public:
        ThreadPool(int32_t threads, std::function handler);

        virtual ~ThreadPool();

        void Submit(Type record);

    private:

    private:
        bool _shutdown;
        int32_t _threads;
        std::function _handler;
        std::vector  _workers;
        ConcurrentQueue  _tasks;
    };




    template
    ThreadPool::ThreadPool(int32_t threads, std::function 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;
                            this->_tasks.Pop(record, true);
                            this->_handler(record);
                        }
                    }
            );
    }


    template
    ThreadPool::~ThreadPool() {
        for (std::thread &worker: _workers)
            worker.join();
    }


    template
    void ThreadPool::Submit(Type record) {
        _tasks.Push(record);
    }




#endif //NET_FRAME_THREAD_POOL_H

 

 

 

你可能感兴趣的:(c/c++/vc)