IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> C++知识库 -> C++实现一个简单的线程池 -> 正文阅读

[C++知识库]C++实现一个简单的线程池

一、设计

线程池应该包括

  • 保存线程的容器,保存任务的容器。
  • 为了能保证避免线程对任务的竞态获取,需要对任务队列进行加锁。
  • 为了使得工作线程感知任务的到来,需要使用条件变量来唤醒工作线程。
  • 任务容器中的任务管理。
  • 任务的处理API。

二、参数选择

使用数组存放线程,链表存放任务。

请添加图片描述
三、类设计

  • 线程池类
template<typename T>
class threadpool
{
public:
    threadpool(int thread_num,int max_request);
    ~threadpool();
    bool append(T* request);    // 在任务队列中添加任务
private:
    static void worker(void* arg);
    void run();
private:
    int m_thread_num;           // 线程池中的线程数
    int m_max_request;          // 任务队列最大保存的任务数
    pthread_t *m_threads;       // 保存线程的容器
    std::list<T*>m_queuework;   // 保存任务的链表
    sem m_sem;                  // 通知工作线程任务到来
    lock m_locker;				// 互斥访问任务队列
};
  • 构造函数
template<typename T>
threadpool<T>::threadpool(int thread_num,int max_request):m_thread_num(thread_num),m_max_request(max_request)
{
    if(thread_num <=0 || max_request <= 0) throw std::exception();
    m_threads = new pthread_t[thread_num];
    if(!m_threads) throw std::exception();
    for(int i = 0;i < thread_num;++i)
    {
        // 创建线程
        if(pthread_create(m_threads + i, NULL,worker,this)!=0)
        {
             delete[] m_threads;
             throw std::exception();
        }
        // 分离线程
        if(pthread_detach(m_threads[i]))
        {
             delete[] m_threads;
             throw std::exception();
        }
    }
}
  • 析构函数
template<typename T>
threadpool<T>::~threadpool()
{
   delete[] m_trheads;
}
  • 添加任务函数
template<typename T>
bool threadpool<T>::append(T* request)
{
    m_locker.lock();
    if(m_queuework.size() > m_max_request)
    {
         m_locker.unlock();
         return false;
    } 
    m_queuework.push_back(request);
    m_locker.unlock();
    m_sem.post();
    return true;
}
  • 任务处理函数
template<typename T>
void* threadpool<T>::worker(void*arg)
{
    threadpool* pool = (threadpool*)arg;
    pool->run();
    return pool;
}

template<typename T>
void threadpool<T>::run()
{
    while(true)
    {
         m_sem.wait();
         m_locker.lock();
         if(m_queuework.empty())
         {
             m_locker.unlock();
             continue;
         }
         T* request = m_queuework.front();
         m_queuework.pop_front();
         m_locker.unlock();      
         request.process();      // 具体任务的处理业务
    }
}
  C++知识库 最新文章
【C++】友元、嵌套类、异常、RTTI、类型转换
通讯录的思路与实现(C语言)
C++PrimerPlus 第七章 函数-C++的编程模块(
Problem C: 算法9-9~9-12:平衡二叉树的基本
MSVC C++ UTF-8编程
C++进阶 多态原理
简单string类c++实现
我的年度总结
【C语言】以深厚地基筑伟岸高楼-基础篇(六
c语言常见错误合集
上一篇文章      下一篇文章      查看所有文章
加:2022-05-21 18:44:48  更:2022-05-21 18:45:42 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/13 12:16:09-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码