c++线程池实现方法
更新时间:2020年4月25日 17:39 点击:1308
本文实例讲述了c++线程池实现方法。分享给大家供大家参考。具体分析如下:
下面这个线程池是我在工作中用到过的,原理还是建立一个任务队列,让多个线程互斥的在队列中取出任务,然后执行,显然,队列是要加锁的
环境:ubuntu linux
文件名:locker.h
#ifndef LOCKER_H_ #define LOCKER_H_ #include "pthread.h" class locker { public: locker(); virtual ~locker(); bool lock(); void unlock(); private: pthread_mutex_t m_mutex; }; #endif /* LOCKER_H_ */
文件名:locker.cpp
#include "locker.h" locker::locker() { pthread_mutex_init(&m_mutex, 0); } locker::~locker() { pthread_mutex_destroy(&m_mutex); } bool locker::lock() { if(0 == pthread_mutex_lock(&m_mutex)) return true; return false; } void locker::unlock() { pthread_mutex_unlock(&m_mutex); }
文件名:task_list.h
#ifndef TASK_LIST_H_ #define TASK_LIST_H_ #include "list" #include "locker.h" #include "netinet/in.h" #include "semaphore.h" using namespace std; typedef void* (*THREAD_FUNC)(void*); // 线程池中运行的任务,对于下行任务,sin中包含目的地址信息 // parm0指向发出数据的对象,parm1指向数据,parm2为数据的长度 typedef struct { THREAD_FUNC func; void* parm0; void* parm1; void* parm2; } task_info; typedef list<task_info*> TASK_LIST; typedef list<task_info*>::iterator PTASK_LIST; class task_list { public: task_list(); virtual ~task_list(); void append_task(task_info* tsk); task_info* fetch_task(); private: TASK_LIST m_tasklist; locker m_lk; sem_t m_sem; }; #endif /* TASK_LIST_H_ */
文件名:task_list.cpp
#include "task_list.h" task_list::task_list() { // Init Semaphore sem_init(&m_sem, 0, 0); m_tasklist.clear(); } task_list::~task_list() { while(!m_tasklist.empty()) { task_info* tr = m_tasklist.front(); m_tasklist.pop_front(); if(tr) delete tr; } // Destroy Semaphore sem_destroy(&m_sem); } void task_list::append_task(task_info* tsk) { // Lock before Modify the list m_lk.lock(); m_tasklist.push_back(tsk); m_lk.unlock(); // Increase the Semaphore sem_post(&m_sem); } task_info* task_list::fetch_task() { task_info* tr = NULL; sem_wait(&m_sem); m_lk.lock(); tr = m_tasklist.front(); m_tasklist.pop_front(); m_lk.unlock(); return tr; }
文件名:thread_pool.h
#ifndef THREAD_POOL_H_ #define THREAD_POOL_H_ #include "task_list.h" #include "pthread.h" #define DEFAULT_THREAD_COUNT 4 #define MAXIMUM_THREAD_COUNT 1000 class thread_pool { public: thread_pool(); virtual ~thread_pool(); int create_threads(int n = DEFAULT_THREAD_COUNT); void delete_threads(); void set_tasklist(task_list* plist); void del_tasklist(); protected: static void* thread_func(void* parm); task_info* get_task(); private: int m_thread_cnt; pthread_t m_pids[MAXIMUM_THREAD_COUNT]; task_list* m_tasklist; }; #endif /* THREAD_POOL_H_ */
文件名:thread_pool.cpp
#include "thread_pool.h" thread_pool::thread_pool() { m_thread_cnt = 0; m_tasklist = NULL; } thread_pool::~thread_pool() { delete_threads(); } task_info* thread_pool::get_task() { task_info* tr; if (m_tasklist) { tr = m_tasklist->fetch_task(); return tr; } return NULL; } void* thread_pool::thread_func(void* parm) { thread_pool *ptp = static_cast<thread_pool*> (parm); task_info *task; while (true) { task = ptp->get_task(); if (task) { (*task->func)(task); //delete task; //func负责释放task_info } } return NULL; } int thread_pool::create_threads(int n) { if (n > MAXIMUM_THREAD_COUNT) n = MAXIMUM_THREAD_COUNT; delete_threads(); for (int i = 0; i < n; i++) { int ret = pthread_create(&m_pids[i], NULL, thread_func, (void*) this); if (ret != 0) break; m_thread_cnt++; } return m_thread_cnt; } void thread_pool::delete_threads() { for (int i = 0; i < m_thread_cnt; i++) { void* retval; pthread_cancel(m_pids[i]); pthread_join(m_pids[i], &retval); } m_thread_cnt = 0; } void thread_pool::set_tasklist(task_list* plist) { m_tasklist = plist; } void thread_pool::del_tasklist() { m_tasklist = NULL; }
文件名:test.cpp
#include "unistd.h" #include "stdio.h" #include "stdlib.h" #include "task_list.h" #include "thread_pool.h" void* fun(void *parm) { task_info* ptk = (task_info*)parm; pid_t tid = pthread_self(); int count = (int)ptk->parm0; printf("count=%d, tid=%d\n", count, tid); return NULL; } int main() { int count = 0; thread_pool tp; task_list tl; tp.create_threads(4 - 1); tp.set_tasklist(&tl); while (1) { task_info* pti = NULL; pti = (task_info *) malloc(sizeof(task_info)); pti->func = fun; pti->parm0 = (void *)count; tl.append_task(pti); count++; sleep(2); } // printf("hello,world\n"); return 0; }
编译运行,我是用ecplise建立的automake工程,所以只要修改一下Makefile.am就可以编译成功了
文件名:Makefile.am
bin_PROGRAMS=test test_SOURCES=test.cpp locker.h locker.cpp \ task_list.h task_list.cpp \ thread_pool.h thread_pool.cpp test_LDADD=-lpthread
执行结果:
count=0, tid=-1219888272 count=1, tid=-1219888272 count=2, tid=-1228280976 count=3, tid=-1236673680 count=4, tid=-1219888272 count=5, tid=-1228280976 count=6, tid=-1236673680 count=7, tid=-1219888272 count=8, tid=-1228280976 count=9, tid=-1236673680
希望本文所述对大家的C++程序设计有所帮助。
上一篇: VC实现对话框窗口任意分割
下一篇: C++ 中滚动条的滚动问题
相关文章
基于springcloud异步线程池、高并发请求feign的解决方案
这篇文章主要介绍了基于springcloud异步线程池、高并发请求feign的解决方案,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-02-25- vector是表示可以改变大小的数组的序列容器,本文主要介绍了C++STL标准库std::vector的使用详解,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2022-03-06
- 这篇文章主要介绍了C++中取余运算的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-02-23
- 这篇文章主要介绍了C++ string常用截取字符串方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-04-25
- 本文通过例子,讲述了C++调用C#的DLL程序的方法,作出了以下总结,下面就让我们一起来学习吧。...2020-06-25
- 本篇文章主要介绍了C++中四种加密算法之AES源代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。...2020-04-25
- 整数拆分,指把一个整数分解成若干个整数的和。本文重点给大家介绍C++ 整数拆分方法详解,非常不错,感兴趣的朋友一起学习吧...2020-04-25
- 这篇文章主要介绍了C++中Sort函数详细解析,sort函数是algorithm库下的一个函数,sort函数是不稳定的,即大小相同的元素在排序后相对顺序可能发生改变...2022-08-18
- 这篇文章主要介绍了C++万能库头文件在vs中的安装步骤(图文),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-02-23
- 这篇文章主要介绍了C++ bitset用法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-04-25
- 本篇文章小编并不是为大家讲解string类型的用法,而是讲解我个人比较好奇的问题,就是string 类型占几个字节...2020-04-25
- 这篇文章主要为大家详细介绍了C++ Eigen库计算矩阵特征值及特征向量,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2020-04-25
- 这篇文章主要介绍了C++ pair的用法实例详解的相关资料,需要的朋友可以参考下...2020-04-25
- 这篇文章主要介绍了Java线程池中的各个参数如何合理设置操作,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教...2021-06-19
- 这篇文章主要介绍了VSCode C++多文件编译的简单使用方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下...2021-03-29
- 虽然C++11引入了智能指针的,但是开发人员在与内存的斗争问题上并没有解放,如果我门实用不当仍然有内存泄漏问题,其中智能指针的循环引用缺陷是最大的问题。下面通过实例代码给大家介绍c++中的循环引用,一起看看吧...2020-04-25
- 在Java中,我们可以利用多线程来最大化地压榨CPU多核计算的能力,下面这篇文章主要给大家介绍了关于java中多线程与线程池基本使用的相关资料,需要的朋友可以参考下...2021-09-13
- 这篇文章主要给大家介绍了关于C++随机点名生成器的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-04-25
- map容器是C++ STL中的重要一员,删除map容器中value为指定元素的问题是我们经常与遇到的一个问题,下面这篇文章主要给大家介绍了关于利用C++如何删除map容器中指定值的元素的相关资料,需要的朋友可以参考借鉴,下面来一起看看吧。...2020-04-25
- 这篇文章主要介绍了C++ 约瑟夫环问题案例详解,本篇文章通过简要的案例,讲解了该项技术的了解与使用,以下就是详细内容,需要的朋友可以参考下...2021-08-15