C++消息队列的实现

(1)消息队列的实现


  
  
  1. #ifndef NET_FRAME_CONCURRENT_QUEUE_H
  2. #define NET_FRAME_CONCURRENT_QUEUE_H
  3. #include <queue>
  4. #include <mutex>
  5. #include <condition_variable>
  6. template< class Type>
  7. /*消息队列实现*/
  8. class ConcurrentQueue {
  9. ConcurrentQueue& operator=( const ConcurrentQueue&) = delete;
  10. ConcurrentQueue( const ConcurrentQueue& other) = delete;
  11. public:
  12. ConcurrentQueue() : _queue(), _mutex(), _condition() { }
  13. virtual ~ConcurrentQueue() { }
  14. void Push(Type record) {
  15. std::lock_guard < std::mutex> lock(_mutex);
  16. _queue.push(record);
  17. _condition.notify_one();
  18. }
  19. bool Pop(Type& record, bool isBlocked = true) {
  20. if (isBlocked) {
  21. std::unique_lock < std::mutex> lock(_mutex);
  22. while (_queue.empty()) {
  23. _condition.wait(lock);
  24. }
  25. }
  26. else // If user wants to retrieve data in non-blocking mode
  27. {
  28. std::lock_guard < std::mutex> lock(_mutex);
  29. if (_queue.empty()) {
  30. return false;
  31. }
  32. }
  33. record = std::move(_queue.front());
  34. _queue.pop();
  35. return true;
  36. }
  37. int32_t Size() {
  38. std::lock_guard < std::mutex> lock(_mutex);
  39. return _queue.size();
  40. }
  41. bool Empty() {
  42. std::lock_guard < std::mutex> lock(_mutex);
  43. return _queue.empty();
  44. }
  45. private:
  46. std:: queue <Type> _queue;
  47. mutable std::mutex _mutex;
  48. std::condition_variable _condition;
  49. };
  50. #endif //NET_FRAME_CONCURRENT_QUEUE_H

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

.h文件如下


  
  
  1. #ifndef NET_FRAME_THREAD_POOL_H
  2. #define NET_FRAME_THREAD_POOL_H
  3. #include "ConcurrentQueue.h"
  4. #include <vector>
  5. #include <queue>
  6. #include <memory>
  7. #include <thread>
  8. #include <mutex>
  9. #include <condition_variable>
  10. #include <future>
  11. #include <functional>
  12. #include <stdexcept>
  13. #define MIN_THREADS 10
  14. template <class Type>
  15. class ThreadPool {
  16. ThreadPool& operator=(const ThreadPool&) = delete;
  17. ThreadPool(const ThreadPool& other) = delete;
  18. public:
  19. ThreadPool(int32_t threads, std::function <void(Type& record)> handler);
  20. virtual ~ThreadPool();
  21. void Submit(Type record);
  22. private:
  23. private:
  24. bool _shutdown;
  25. int32_t _threads;
  26. std::function <void(Type& record)> _handler;
  27. std::vector <std::thread> _workers;
  28. ConcurrentQueue <Type> _tasks;
  29. };
  30.     template <class Type>
  31.     ThreadPool <Type>::ThreadPool(int32_t threads, std::function <void(Type &record)> handler)
  32.             : _shutdown(false),
  33.               _threads(threads),
  34.               _handler(handler),
  35.               _workers(),
  36.               _tasks() {
  37.         if (_threads < MIN_THREADS)
  38.             _threads = MIN_THREADS;
  39.         for (int32_t i = 0; i < _threads; ++i)
  40.             _workers.emplace_back(
  41.                     [this] {
  42.                         while (!_shutdown) {
  43.                             Type record;
  44.                             _tasks.Pop(record, true);
  45.                             _handler(record);
  46.                         }
  47.                     }
  48.             );
  49.     }
  50.     template<class Type>
  51.     ThreadPool <Type>::~ThreadPool() {
  52.         for (std::thread &worker: _workers)
  53.             worker.join();
  54.     }
  55.     template <class Type>
  56.     void ThreadPool <Type>::Submit(Type record) {
  57.         _tasks.Push(record);
  58.     }
  59. #endif //NET_FRAME_THREAD_POOL_H

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值