半同步半异步线程池

//Syncqueue.h:

#include<list>

#include<mutex>
#include<thread>
#include<condition_variable>
#include<iostream>
using namespace std;
template<typename T>
class SyncQueue
{
public:
SyncQueue(int maxSize): m_maxSize(maxSize), m_needStop(false){}
void Put(const T& x)
{
Add(x);
}
void Put(T&& x)
{
Add(std::forward<T>(x));
}

void Take( list<T>& list)
{
std::unique_lock<std::mutex> locker(m_mutex);
m_notFull.wait(locker, [this]{return !m_needStop && NotEmpty(); });
if(m_needStop)
    return;
list = std::move(m_queue);
locker.unlock();
m_notFull.notify_one();
}
void Take(T& t)
{
std::unique_lock<std::mutex> locker(m_mutex);
m_notEmpty.wait(locker, [this]{return !m_needStop && NotEmpty(); });
if(m_needStop)
     return;
t = m_queue.front();
m_queue.pop_front();
locker.unlock();
m_notFull.notify_one();
}
void Stop()
{
        { 
               std::lock_guard<std::mutex> locker(m_mutex);
               m_needStop = true;
}
       m_notFull.notify_all();
       m_notEmpty.notify_all();
}
bool Empty()
{
std::lock_guard<std::mutex> locker(m_mutex);
return m_queue.empty();
}
bool Full()
{
return m_queue.size() == m_maxSize;
}
size_t Size()
{
std::lock_guard<std::mutex> locker(m_mutex);
return m_queue.size();
}
int Count()
{
return m_queue.size();
}
private:
         bool NotFull() const
{
     bool full = m_queue.size() >= m_maxSize;
 if (full)
      cout << "缓冲区满了 , 请等待...." << endl;
 return !full;
}
bool NotEmpty() const
{
bool empty = m_queue.empty();
if (empty)
    cout << "缓冲区空了, 需要等待...异步线程的ID:" << 
 this_thread::get_id() << endl;
return !empty;
}
template<typename F>
void Add(F&& x)
{
std::unique_lock<std::mutex> locker(m_mutex);
m_notFull.wait(locker, [this]{return m_needStop || NotFull();});
if (m_needStop)
   return;
m_queue.push_back(std::forward<F>(x));
locker.unlock(); 
m_notEmpty.notify_one();
}
private:
std::list<T> m_queue;
std::mutex m_mutex;
std::condition_variable m_notEmpty;
std::condition_variable m_notFull;
int m_maxSize;
bool m_needStop;

};

ThreadPool.h

#include<list>
#include<thread>
#include<functional>
#include<memory>
#include<atomic>
#include"SyncQueue.h"


const int MaxTaskCount = 100;
class ThreadPool
{
public:
        using Task = std::function<void()>;
        
        ThreadPool(int numThreads = std::thread::hardware_concurrency()) : m_queue
          (MaxTaskCount)
          {
          Start(numThreads);
 }
 
 ~ThreadPool(void)
 {
  Stop();
 }
 
 void Stop()
 {
  std::call_once(m_flag, [this]{StopThreadGroup();});
 }
 
 void AddTask( Task&& task)
 {
  m_queue.Put(std::forward<Task>(task));
 }
private:
void RunINThread()
{
while (m_running)
{
std::list<Task> list;
m_queue.Take(list);
for(auto & task : list)
{
if(!m_running)
  return;
task();
}
}
}
void Start(int numThreads)
{
m_running = true;
for (int i = 0;i < numThreads; ++i)
{
m_threadgroup.push_back(std::make_shared<std::thread>(&ThreadPool::RunINThread, this));
}
}

void StopThreadGroup()
{
m_queue.Stop();
m_running = false;
for (auto thread : m_threadgroup)
{
if (thread)
  thread->join();
}
m_threadgroup.clear();
}
private:
std::list<std::shared_ptr<std::thread>> m_threadgroup;
SyncQueue<Task> m_queue;
atomic_bool m_running;
std::once_flag m_flag;
};
  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值