c++实现的线程池

本文介绍了一个基于Ubuntu Linux的线程池实现,通过使用互斥锁、信号量等技术,有效地管理并发任务,包括任务列表的增删查改操作及线程的创建与销毁。实现了线程间任务的高效分配与执行。

下面这个线程池是我在工作中用到过的,原理还是建立一个任务队列,让多个线程互斥的在队列中取出任务,然后执行,显然,队列是要加锁的

环境: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

转载请注明出处: http://blog.youkuaiyun.com/liujian0616/article/details/7954381

<think>好的,我现在需要帮助用户找到如何用C++实现线程池的示例代码或教程。首先,用户提到了他们之前看到的引用内容,里面有两个引用[^1]和[^2],都是关于C++线程池实现。我需要先仔细看看这两个引用里的内容,看看是否能直接提供代码或者教程的链接或具体步骤。 引用[1]中的代码示例展示了使用一个名为zThreadPool.h的头文件,主函数中创建了一个有4个线程的线程池,并添加了8个任务。任务函数是functionToRunInThread,它接收一个任务ID并打印线程ID和任务ID。看起来这个示例代码可能来自用户提到的某个教程或者项目,但用户提供的引用中没有给出更多的上下文或链接。不过代码本身已经比较完整,可以作为参考。 引用[2]则提到了另一个实现,其中定义了一个printTask任务,主函数创建了4个线程的线程池并提交了10个任务。同样,这里提到了stopPool函数来等待线程执行完毕。这可能意味着第二个实现有更完整的生命周期管理,比如启动和停止线程池的方法。 用户的需求是希望得到实现线程池的示例代码或教程。由于用户提供的引用中已经包含了部分代码,我应该将这些代码整理出来,并解释关键部分。同时,可能需要补充一些常见的实现步骤,比如线程池的基本结构、任务队列、工作线程的管理、同步机制(如互斥锁和条件变量)等。 接下来,我需要确保回答的结构清晰,分步骤说明如何实现线程池。首先,可能需要一个简单的实现示例,基于用户提供的引用代码,然后解释各部分的功能。此外,还需要提到一些优化方法或注意事项,比如异常处理、资源管理、任务调度策略等。 需要注意的是,用户可能对C++的线程库不太熟悉,所以代码示例中应该使用标准的C++11或更高版本的特性,比如std::thread、std::mutex、std::condition_variable等。同时,示例代码需要足够简洁,易于理解,但也要覆盖线程池的核心部分。 另外,用户可能希望了解如何添加任务到线程池中,以及如何正确关闭线程池,避免资源泄漏。引用中的代码没有展示线程池的停止机制,但引用[2]提到了stopPool函数,所以可能需要补充这部分内容,比如使用标志位通知线程退出,并join所有线程。 最后,根据用户的要求,生成相关问题。这些问题可能涉及线程池的工作原理、性能优化、任务调度策略、异常处理等,帮助用户进一步深入学习。 总结来说,我需要整理用户提供的代码示例,补充关键部分的解释,并给出一个完整的实现步骤,同时指出注意事项和优化建议。然后根据内容生成相关问题,帮助用户深化理解。</think>以下是一个基于C++11标准的线程池实现示例及解析,包含核心功能模块和代码注释: ```cpp #include <iostream> #include <vector> #include <queue> #include <thread> #include <mutex> #include <condition_variable> #include <functional> class ThreadPool { public: explicit ThreadPool(size_t threadNum) : stop(false) { for(size_t i = 0; i < threadNum; ++i) { workers.emplace_back([this] { for(;;) { std::function<void()> task; { std::unique_lock<std::mutex> lock(this->queueMutex); this->condition.wait(lock, [this]{ return this->stop || !this->tasks.empty(); }); if(this->stop && this->tasks.empty()) return; task = std::move(this->tasks.front()); this->tasks.pop(); } task(); } }); } } template<class F, class... Args> void enqueue(F&& f, Args&&... args) { { std::unique_lock<std::mutex> lock(queueMutex); tasks.emplace([=]{ f(args...); }); } condition.notify_one(); } ~ThreadPool() { { std::unique_lock<std::mutex> lock(queueMutex); stop = true; } condition.notify_all(); for(std::thread &worker : workers) worker.join(); } private: std::vector<std::thread> workers; std::queue<std::function<void()>> tasks; std::mutex queueMutex; std::condition_variable condition; bool stop; }; // 使用示例 void printTask(int taskId) { std::cout << "Thread " << std::this_thread::get_id() << " processing task " << taskId << std::endl; } int main() { ThreadPool pool(4); // 提交10个任务 for(int i = 0; i < 10; ++i) { pool.enqueue(printTask, i); } return 0; } ``` ### 实现解析 1. **线程管理** - 通过`std::vector<std::thread>`存储工作线程 - 构造函数中创建指定数量的工作线程,每个线程循环等待任务分配 2. **任务队列** - 使用`std::queue<std::function<void()>>`存储待执行任务 - 采用先进先出(FIFO)的任务调度策略 3. **同步机制** - `std::mutex`保护任务队列的并发访问 - `std::condition_variable`实现线程间的任务通知机制 4. **任务提交接口** - 模板方法`enqueue`支持任意参数类型的任务提交 - 使用完美转发(perfect forwarding)保持参数类型完整性 5. **安全关闭** - 析构函数中设置停止标志位 - 唤醒所有线程并等待任务完成 ### 优化建议 1. 增加任务优先级机制 2. 实现动态线程数量调整 3. 添加异常处理机制 4. 支持任务返回值的获取(可通过`std::future`实现
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值