ThreadPool.h

#ifndef __THREADPOOL_H
#define __THREADPOOL_H #define HAVE_STRUCT_TIMESPEC //#include "servant/Application.h"
#include <vector>
#include <string>
#include <pthread.h> using namespace std; /**
* 执行任务的类,设置任务数据、定义执行方法(纯虚函数)
*/
class CTask
{
protected:
string m_strTaskName; //任务的名称
void* m_ptrData; //具体数据
public:
CTask() {}
CTask(string taskName)
{
m_strTaskName = taskName;
m_ptrData = NULL;
}
virtual int Run() = 0; //任务执行方法
void SetData(void* data); //设置任务数据 public:
virtual ~CTask() {}
}; /**
* 线程结构体
*/
struct CThread
{
pthread_t pthread_id; //线程id
int iStat; //线程状态
CThread()
: iStat(0)
{ }
bool operator == (const CThread &obj) const
{
return (long)&pthread_id == (long)&obj.pthread_id;
}
}; /**
* 线程池管理类的实现
*/
class CThreadPool
{
public:
CThreadPool(int threadNum = 10);
int AddTask(CTask *task); //把任务添加到任务队列中
int getTaskSize(); //获取当前任务队列中的任务数
int StopAll(); //使线程池中的线程退出 protected:
int Create(); //创建线程池中的线程 static void* ThreadFunc(void * threadData); //新线程的线程回调函数 static int MoveToIdle(CThread *pThread); //线程执行结束后,状态置为空闲0
static int MoveToBusy(CThread *pThread); //线程开始执行,状态置为运行1 private:
static vector<CTask*> m_vecTaskList; //任务列表 static bool shutdown; //线程退出标志
int m_iThreadNum; //线程池中启动的线程数
static vector<CThread> m_vecThread; //线程列表 static pthread_mutex_t m_pthreadMutex; //线程同步锁
static pthread_cond_t m_pthreadCond; //线程同步的条件变量
}; #endif

  

ThreadPool.cpp

#include "ThreadPool.h"
#include <iostream>
#include <algorithm>
using namespace std; void CTask::SetData(void * data)
{
m_ptrData = data;
} vector<CTask*> CThreadPool::m_vecTaskList; //任务列表
bool CThreadPool::shutdown = false;
vector<CThread> CThreadPool::m_vecThread; //线程列表 pthread_mutex_t CThreadPool::m_pthreadMutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t CThreadPool::m_pthreadCond = PTHREAD_COND_INITIALIZER; /**
* 线程池管理类构造函数
*/
CThreadPool::CThreadPool(int threadNum)
{
this->m_iThreadNum = threadNum;
cout << "threadNum:" << threadNum << " threads will be created." << endl;
Create(); //创建线程
} /**
* 创建线程
*/
int CThreadPool::Create()
{
m_vecThread.resize(m_iThreadNum);
for (size_t i = 0; i < m_vecThread.size(); i++)
{
pthread_create(&m_vecThread[i].pthread_id, NULL, ThreadFunc, &m_vecThread[i]);
}
return 0;
} /**
* 线程回调函数
*/
void* CThreadPool::ThreadFunc(void* threadData)
{
CThread *pThread = (CThread*)threadData;
while (1)
{
pthread_mutex_lock(&m_pthreadMutex); //lock
while (m_vecTaskList.size() == 0 && !shutdown)
{
pthread_cond_wait(&m_pthreadCond, &m_pthreadMutex);
/*
pthread_cond_wait前要先加锁
pthread_cond_wait把线程放进阻塞队列后,内部会解锁,然后等待条件变量被其它线程唤醒
pthread_cond_wait被唤醒后会再自动加锁
*/
} if (shutdown)
{
pthread_mutex_unlock(&m_pthreadMutex);
cout << "thread:" << (long)&pThread->pthread_id << " will exit." << endl;
pthread_exit(NULL);
} //线程状态置1
MoveToBusy(pThread); //取出一个任务
CTask* task = NULL;
vector<CTask*>::iterator iter = m_vecTaskList.begin();
if (iter != m_vecTaskList.end())
{
task = *iter;
m_vecTaskList.erase(iter);
} pthread_mutex_unlock(&m_pthreadMutex); //unlock //执行任务
if (task)
{
task->Run();
}
//线程状态置0
MoveToIdle(pThread);
}
return (void*)0;
} int CThreadPool::MoveToIdle(CThread *pThread)
{
vector<CThread>::iterator iter_thread = std::find(m_vecThread.begin(), m_vecThread.end(), *pThread);
if (iter_thread != m_vecThread.end())
{
iter_thread->iStat = 0;
cout << "tid:" << (long)&pThread->pthread_id << " idle." << endl;
}
return 0;
} int CThreadPool::MoveToBusy(CThread *pThread)
{
vector<CThread>::iterator iter_thread = std::find(m_vecThread.begin(), m_vecThread.end(), *pThread);
if (iter_thread != m_vecThread.end())
{
iter_thread->iStat = 1;
cout << "tid:" << (long)&pThread->pthread_id << " run." << endl;
}
return 0;
} /**
* 往任务队列里边添加任务并发出线程同步信号
*/
int CThreadPool::AddTask(CTask *task)
{
pthread_mutex_lock(&m_pthreadMutex);
this->m_vecTaskList.push_back(task);
pthread_cond_signal(&m_pthreadCond);
pthread_mutex_unlock(&m_pthreadMutex);
return 0;
} /**
* 获取当前队列中任务数
*/
int CThreadPool::getTaskSize()
{
return m_vecTaskList.size();
} /**
* 停止所有线程
*/
int CThreadPool::StopAll()
{
/** 避免重复调用 */
if (shutdown)
{
return -1;
}
cout << "All threads will be stoped." << endl;
/** 唤醒所有等待线程,线程池要销毁了 */
shutdown = true;
pthread_cond_broadcast(&m_pthreadCond); /** 阻塞等待线程退出,否则就成僵尸了 */
for (size_t i = 0; i < m_vecThread.size(); i++)
{
pthread_join(m_vecThread[i].pthread_id, NULL);
}
m_vecThread.clear(); /** 销毁条件变量和互斥体 */
pthread_mutex_destroy(&m_pthreadMutex);
pthread_cond_destroy(&m_pthreadCond); return 0;
}

  

main.cpp

#include <iostream>
#include "ThreadPool.h" using namespace std; class CMyTask : public CTask
{
public:
CMyTask() {} inline int Run()
{
cout << (char*)this->m_ptrData << endl;
return 0;
}
}; int main()
{
CThreadPool threadPool(10); CMyTask taskObj; char szTmp[] = "this is the first thread running";
taskObj.SetData((void*)szTmp); for (int i = 0; i < 10; i++)
{
threadPool.AddTask(&taskObj);
} while (1)
{
cout << "there are still " << threadPool.getTaskSize() << " tasks need to handle" << endl;
if (threadPool.getTaskSize() == 0)
{
if (threadPool.StopAll() == -1)
{
cout << "Now I will exit from main" << endl;
return 0;
}
}
}
return 0;
}

  

makeflie

TARGET:=threadpool
INC:= -I./
LIB_PATH:=
LIB:= -lpthread CFLAGS:=-Wall -g -O0 -D_REENTRANT -Wl,-rpath=./ $(INC) $(LIB_PATH)
CPPFLAGS:=$(CFLAGS) SRC:=$(shell echo *.cpp)
OBJ:=$(patsubst %.cpp,%.o,$(SRC)) all: $(TARGET) $(TARGET): $(OBJ)
$(CXX) $^ $(CFLAGS) $(LIB) -o $@ clean:
rm -f $(OBJ)
rm -f $(TARGET)

  

windows下配置 pthread 参见:https://blog.csdn.net/qianchenglenger/article/details/16907821

线程同步、条件变量说明参见:https://www.cnblogs.com/zhangxuan/p/6526854.html

c++线程池小例子的更多相关文章

  1. Java 多线程编程之九:使用 Executors 和 ThreadPoolExecutor 实现的 Java 线程池的例子

    线程池用来管理工作线程的数量,它持有一个等待被执行的线程的队列.         java.util.concurrent.Executors 提供了 java.util.concurrent.Exe ...

  2. 13 并发编程-(线程)-异步调用与回调机制&进程池线程池小练习

    #提交任务的两种方式 #1.同步调用:提交完任务后,就在原地等待任务执行完毕,拿到结果,再执行下一行代码,导致程序是串行执行 一.提交任务的两种方式 1.同步调用:提交任务后,就在原地等待任务完毕,拿 ...

  3. java 线程池简单例子

    package com.hra.riskprice; import com.hra.riskprice.SysEnum.Factor_Type; import com.hra.riskprice.po ...

  4. Java ExecutorService四种线程池的例子与说明

    1.new Thread的弊端 执行一个异步任务你还只是如下new Thread吗? new Thread(new Runnable() { @Override public void run() { ...

  5. Java 1.ExecutorService四种线程池的例子与说明

    1.new Thread的弊端 执行一个异步任务你还只是如下new Thread吗? new Thread(new Runnable() { @Override public void run() { ...

  6. Java ExecutorService四种线程池的例子与说明(转发)

    1.new Thread的弊端 执行一个异步任务你还只是如下new Thread吗? new Thread(new Runnable() { @Override public void run() { ...

  7. java并发编程基础——线程池

    线程池 由于启动一个线程要与操作系统交互,所以系统启动一个新的线程的成本是比较高的.在这种情况下,使用线程池可以很好的提升性能,特别是程序中涉及创建大量生命周期很短暂的线程时. 与数据库连接池类似,线 ...

  8. c++封装编写线程池

    在csapp学习或者其他linux底层编程的过程中,一般都会举一些多线程或多进程的例子,配合底层同步原语.系统调用api来解释怎么创建多线程/多进程. 但是这些例子和实际项目中所用到的多线程/多进程编 ...

  9. 手写线程池,对照学习ThreadPoolExecutor线程池实现原理!

    作者:小傅哥 博客:https://bugstack.cn Github:https://github.com/fuzhengwei/CodeGuide/wiki 沉淀.分享.成长,让自己和他人都能有 ...

随机推荐

  1. [蓝点ZigBee] Zstack 之按键驱动以及控制LED灯 ZigBee/CC2530 视频资料

    这一节主要演示如何在Zstack 下根据板子的不同修改按键驱动,实际演示的时候代码跳动比较多,建议大家除了看视频资料以外,还需要在网上找一下相关资料进一步学习. 视频总览:http://bphero. ...

  2. maven命令注册jar包到maven仓库

    首先需要具备maven环境 在cmd命令行执行以下命令注册jar包 mvn install:install-file -Dfile=E:\aliyun-java-sdk-dysmsapi-1.0.0. ...

  3. me 云面试

    元祖的特点: 1.元组内的元素,不可以增加,删除,只能访问,这个是元祖的特性,比较安全.类似于字符串.但是我们可以对整个元祖进行删除.使用del内置函数 2.当元祖内只有一个元素的时候,需要加逗号消除 ...

  4. C++ Primer 与“类”有关的注意事项总结

    C++ 与"类"有关的注意事项总结(一) 1. 除了静态 static 数据成员外,数据成员不能在类体中被显式地初始化. 例如 : class First { int memi = ...

  5. ReactNative用指定的设备/模拟器运行项目

    命令行中React native项目目录下键入react-native run-ios会启动iOS模拟器, 默认是使用iPhone6,如果想要试用其他版本的模拟器则需要在react-native ru ...

  6. [机器学习入门篇]-Logistic函数与Softmax函数

    1.Logistic函数 在维基百科中,对logistic函数这样介绍道: A logistic function or logistic curve is a common "S" ...

  7. Windows10关机问题----只有“睡眠”、“更新并重启”、“更新并关机”,但是又不想更新,解决办法

    最近的一个问题,电脑关机的时候发现,只有“睡眠”.“更新并重启”.“更新并关机” 内心很是煎熬.... 尝试了N种方式,然后总结如下: 第一种方式:(表示自己window的系统用着挺好,力荐) 1.打 ...

  8. jQueryUI详解

    引入文件下载地址:http://jqueryui.com/download/ dialog常见的参数: 常用参数: 属性 类型 说明 Boolean autoOpen 属性 设置该组件被调用时的打开状 ...

  9. Vue(二十)项目初始化步骤

    提:需要安装 node.js / npm淘宝镜像 / webpack / vue-cli脚手架构建工具 1.创建项目 - vue init webpack framework https://gith ...

  10. 基于SwiperJs的H5/移动端下拉刷新上拉加载更多

    最早时,公司的H5项目中曾用过点击一个"加载更多"的DOM元素来实现分页的功能,后来又用过网上有人写的一个上拉加载更多的插件,那个插件是页面将要滚动到底部时就自动请求数据并插入到页 ...