• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • JavaScript
  • ASP.NET
  • PHP
  • 正则表达式
  • AJAX
  • JSP
  • ASP
  • Flex
  • XML
  • 编程技巧
  • Android
  • swift
  • C#教程
  • vb
  • vb.net
  • C语言
  • Java
  • Delphi
  • 易语言
  • vc/mfc
  • 嵌入式开发
  • 游戏开发
  • ios
  • 编程问答
  • 汇编语言
  • 微信小程序
  • 数据结构
  • OpenGL
  • 架构设计
  • qt
  • 微信公众号
您的位置:首页 > 程序设计 >C语言 > c++线程池实现方法

c++线程池实现方法

作者:liujian0616 字体:[增加 减小] 来源:互联网 时间:2017-05-28

liujian0616 通过本文主要向大家介绍了c++线程池,队列线程池c++,c++线程池原理,c++创建线程,c++多线程等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

本文实例讲述了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_ */ 

</div>

文件名: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); 
}

</div>

文件名: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_ */

</div>

文件名: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; 
}

</div>

文件名: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_ */ 

</div>

文件名: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; 
}

</div>

文件名: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; 
} 

</div>

编译运行,我是用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 

</div>

执行结果:

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 

</div>

希望本文所述对大家的C++程序设计有所帮助。

</div>
分享到:QQ空间新浪微博腾讯微博微信百度贴吧QQ好友复制网址打印

您可能想查找下面的文章:

  • c++实现简单的线程池
  • c++线程池实现方法
  • c++实现简单的线程池
  • C++线程同步实例分析
  • C++线程池的简单实现方法
  • c++版线程池和任务池示例

相关文章

  • 2017-05-28C++空类及没有成员变量的类的大小实例分析
  • 2017-05-28C++递归删除一个目录实例
  • 2017-05-28C++ 静态成员的类内初始化详解及实例代码
  • 2017-05-28C语言编程中统计输入的行数以及单词个数的方法
  • 2017-05-28C++虚函数及虚函数表简析
  • 2022-04-30C语言标识符、关键字、注释、表达式和语句
  • 2017-05-28自己实现strcpy函数的实现方法
  • 2017-05-28C++利用容器查找重复列功能实现
  • 2017-05-28C++开发的Redis数据导入工具优化
  • 2017-05-28c++遍历lua table示例

文章分类

  • JavaScript
  • ASP.NET
  • PHP
  • 正则表达式
  • AJAX
  • JSP
  • ASP
  • Flex
  • XML
  • 编程技巧
  • Android
  • swift
  • C#教程
  • vb
  • vb.net
  • C语言
  • Java
  • Delphi
  • 易语言
  • vc/mfc
  • 嵌入式开发
  • 游戏开发
  • ios
  • 编程问答
  • 汇编语言
  • 微信小程序
  • 数据结构
  • OpenGL
  • 架构设计
  • qt
  • 微信公众号

最近更新的内容

    • c++实现简单的线程池
    • C语言小程序 如何判断三角型类型
    • 算法详解之回溯法具体实现
    • 深入解析C++中的mutable关键字
    • C# interface与delegate效能比较的深入解析
    • 浅谈c语言中转义字符的用法及注意事项
    • C++非递归遍历磁盘文件和递归遍历磁盘文件的程序示例
    • c语言可变参数实现示例
    • C++中自定义sleep、条件变量sleep实例
    • 电脑开机时间的计算代码

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有