线程池 - C++

1. 基本概念

线程池(thread pool): 一种线程使用模式,线程过多会带来调度开销,进而影响缓存局部性和整体性能。而线程池维护着多个线程,等待着监督管理者分配可并发执行的任务。这避免了在短时间任务创建与销毁线程的代价。线程池不仅能够保证内核的充分利用,还能防止过分调度。可用线程数据取决于可用的并发处理器、处理器内核、内存、网络sockets等数量。

2. 线程池的组成

2.1 线程池管理器

创建一定数量的线程,启动线程,调配任务,管理着线程池。
线程池目前只需要启动(start()),停止方法(stop()),及任务添加方法(addTask).
start()创建一定数量的线程池,进入线程循环.
stop()停止所有的线程循环,回收所有资源.
addTask()添加任务.

2.2 工作线程

线程池中线程,在线程池中等待并执行分配任务.
该文选用条件变量实现等待和通知机制

2.3 任务接口

添加任务接口,以供工作线程调度任务的执行.

2.4 任务队列

用于存放没有处理的任务,提供一种缓冲机制,同时任务队列具有调度功能,高级优先的任务放在任务队列前面;本文选用priority_queue与pair的结合用作任务优先队列结构.

3. 线程池工作的四种情况

假设我们的线程池大小为3,任务队列目前不做大小限制。

3.1 主程序当前没有任务要执行,线程池中任务队列为空闲状态

下面情况下所有工作线程处于空闲的等待状态,任务缓冲队列为空.

3.2 主程序添加小于等于线程池中线程数量得任务

基于3.1情况,所有的工作线程已处于等待状态,主线程开始添加三个任务,添加后通知(notif())唤醒线程池中的线程开始取(take())任务执行。此时的任务缓冲队列还是空。

3.3 主程序添加任务数量大于当前线程池中线程数量的任务

基于3.2情况,所有工作线程都在工作中,主线程开始添加第四个任务,添加后发现现在线程池中线程用完了,于是存入任务缓冲队列。工作线程空闲后主动从任务队列取任务执行。

3.4 主程序添加任务数量大于当前线程池中线程数量的任务,且任务缓冲队列已满

此情况发生情形3且设置了任务缓冲队列大小后面,主程序添加第N个任务,添加后发现线程池中线程已经用完了,任务缓冲队列已满,于是进入等待状态,等待任务缓冲队列中任务腾空通知。但是这种情形会阻塞主线程,本文不限制任务队列的大小,必要时再优化。

4. 线程池的C++实现

参考连接: Thread poolThreadPool


线程池的主要组成由三个部分构成:

  • 任务队列(Task Quene)
  • 线程池(Thread Pool)
  • 完成队列(Completed Tasks)

等待通知机制通过条件变量实现,Logger和CurrentThread,用于调试,可以无视。

#ifndef _THREADPOOL_HH
#define _THREADPOOL_HH

#include <vector>
#include <utility>
#include <queue>
#include <thread>
#include <functional>
#include <mutex>

#include "Condition.hh"

class ThreadPool{
public:
  static const int kInitThreadsSize = 3;
  enum taskPriorityE { level0, level1, level2, };
  typedef std::function<void()> Task;
  typedef std::pair<taskPriorityE, Task> TaskPair;

  ThreadPool();
  ~ThreadPool();

  void start();
  void stop();
  void addTask(const Task&);
  void addTask(const TaskPair&);

private:
  ThreadPool(const ThreadPool&);//禁止复制拷贝.
  const ThreadPool& operator=(const ThreadPool&);

  struct TaskPriorityCmp
  {
    bool operator()(const ThreadPool::TaskPair p1, const ThreadPool::TaskPair p2)
    {
        return p1.first > p2.first; //first的小值优先
    }
  };

  void threadLoop();
  Task take();

  typedef std::vector<std::thread*> Threads;
  typedef std::priority_queue<TaskPair, std::vector<TaskPair>, TaskPriorityCmp> Tasks;

  Threads m_threads;
  Tasks m_tasks;

  std::mutex m_mutex;
  Condition m_cond;
  bool m_isStarted;
};

#endif

//Cpp

#include <assert.h>

#include "Logger.hh" // debug
#include "CurrentThread.hh" // debug
#include "ThreadPool.hh"

ThreadPool::ThreadPool()
  :m_mutex(),
  m_cond(m_mutex),
  m_isStarted(false)
{

}

ThreadPool::~ThreadPool()
{
  if(m_isStarted)
  {
    stop();
  }
}

void ThreadPool::start()
{
  assert(m_threads.empty());
  m_isStarted = true;
  m_threads.reserve(kInitThreadsSize);
  for (int i = 0; i < kInitThreadsSize; ++i)
  {
    m_threads.push_back(new std::thread(std::bind(&ThreadPool::threadLoop, this)));
  }

}

void ThreadPool::stop()
{
  LOG_TRACE << "ThreadPool::stop() stop.";
  {
    std::unique_lock<std::mutex> lock(m_mutex);
    m_isStarted = false;
    m_cond.notifyAll();
    LOG_TRACE << "ThreadPool::stop() notifyAll().";
  }

  for (Threads::iterator it = m_threads.begin(); it != m_threads.end() ; ++it)
  {
    (*it)->join();
    delete *it;
  }
  m_threads.clear();
}


void ThreadPool::threadLoop()
{
  LOG_TRACE << "ThreadPool::threadLoop() tid : " << CurrentThread::tid() << " start.";
  while(m_isStarted)
  {
    Task task = take();
    if(task)
    {
      task();
    }
  }
  LOG_TRACE << "ThreadPool::threadLoop() tid : " << CurrentThread::tid() << " exit.";
}

void ThreadPool::addTask(const Task& task)
{
  std::unique_lock<std::mutex> lock(m_mutex);
  /*while(m_tasks.isFull())
    {//when m_tasks have maxsize
      cond2.wait();
    }
  */
  TaskPair taskPair(level2, task);
  m_tasks.push(taskPair);
  m_cond.notify();
}

void ThreadPool::addTask(const TaskPair& taskPair)
{
  std::unique_lock<std::mutex> lock(m_mutex);
  /*while(m_tasks.isFull())
    {//when m_tasks have maxsize
      cond2.wait();
    }
  */
  m_tasks.push(taskPair);
  m_cond.notify();
}

ThreadPool::Task ThreadPool::take()
{
  std::unique_lock<std::mutex> lock(m_mutex);
  //always use a while-loop, due to spurious wakeup
  while(m_tasks.empty() && m_isStarted)
  {
    LOG_TRACE << "ThreadPool::take() tid : " << CurrentThread::tid() << " wait.";
    m_cond.wait(lock);
  }

  LOG_TRACE << "ThreadPool::take() tid : " << CurrentThread::tid() << " wakeup.";

  Task task;
  Tasks::size_type size = m_tasks.size();
  if(!m_tasks.empty() && m_isStarted)
  {
    task = m_tasks.top().second;
    m_tasks.pop();
    assert(size - 1 == m_tasks.size());
    /*if (TaskQueueSize_ > 0)
    {
      cond2.notify();
    }*/
  }

  return task;

}

4.1 队列

队列作为先进先出的数据结构,当有可用的工作时,线程从队列中获取工作并执行。如果两个线程同时执行相同的工作会出现程序崩溃。为了避免这种问题,需要再标准C++ Queue上实现一个包装器,使用mutex来限制并发访问。

void enqueue(T& t) {
    std::unique_lock<std::mutex> lock(m_mutex);
    m_queue.push(t);
}

要排队做的第一件事情就是锁定互斥锁来确保没有其他人正在访问该资源。然后,将元素推送到队列当中。当锁超出范围时,它会自动释放,这样使Queue线程安全,因此不用担心许多线程在相同时间访问或者修改它。

4.2 提交函数

线程池最重要的方法是负责向队列添加任务。

5. 测试程序

5.1 start()、stop()

测试线程池基本的创建退出工作,以及检测资源是否回收正常。

int main(){
  {
  ThreadPool threadPool;
  threadPool.start();
  getchar();}

  getchar();
  return 0;
}
./test.out 
2018-11-25 16:50:36.054805 [TRACE] [ThreadPool.cpp:53] [threadLoop] ThreadPool::threadLoop() tid : 3680 start.
2018-11-25 16:50:36.054855 [TRACE] [ThreadPool.cpp:72] [take] ThreadPool::take() tid : 3680 wait.
2018-11-25 16:50:36.055633 [TRACE] [ThreadPool.cpp:53] [threadLoop] ThreadPool::threadLoop() tid : 3679 start.
2018-11-25 16:50:36.055676 [TRACE] [ThreadPool.cpp:72] [take] ThreadPool::take() tid : 3679 wait.
2018-11-25 16:50:36.055641 [TRACE] [ThreadPool.cpp:53] [threadLoop] ThreadPool::threadLoop() tid : 3681 start.
2018-11-25 16:50:36.055701 [TRACE] [ThreadPool.cpp:72] [take] ThreadPool::take() tid : 3681 wait.
2018-11-25 16:50:36.055736 [TRACE] [ThreadPool.cpp:53] [threadLoop] ThreadPool::threadLoop() tid : 3682 start.
2018-11-25 16:50:36.055746 [TRACE] [ThreadPool.cpp:72] [take] ThreadPool::take() tid : 3682 wait.

2018-11-25 16:51:01.411792 [TRACE] [ThreadPool.cpp:36] [stop] ThreadPool::stop() stop.
2018-11-25 16:51:01.411863 [TRACE] [ThreadPool.cpp:39] [stop] ThreadPool::stop() notifyAll().
2018-11-25 16:51:01.411877 [TRACE] [ThreadPool.cpp:76] [take] ThreadPool::take() tid : 3680 wakeup.
2018-11-25 16:51:01.411883 [TRACE] [ThreadPool.cpp:62] [threadLoop] ThreadPool::threadLoop() tid : 3680 exit.
2018-11-25 16:51:01.412062 [TRACE] [ThreadPool.cpp:76] [take] ThreadPool::take() tid : 3682 wakeup.
2018-11-25 16:51:01.412110 [TRACE] [ThreadPool.cpp:62] [threadLoop] ThreadPool::threadLoop() tid : 3682 exit.
2018-11-25 16:51:01.413052 [TRACE] [ThreadPool.cpp:76] [take] ThreadPool::take() tid : 3679 wakeup.
2018-11-25 16:51:01.413098 [TRACE] [ThreadPool.cpp:62] [threadLoop] ThreadPool::threadLoop() tid : 3679 exit.
2018-11-25 16:51:01.413112 [TRACE] [ThreadPool.cpp:76] [take] ThreadPool::take() tid : 3681 wakeup.
2018-11-25 16:51:01.413141 [TRACE] [ThreadPool.cpp:62] [threadLoop] ThreadPool::threadLoop() tid : 3681 exit.

5.2 addTask()、 PriorityTaskQueue

测试添加任务接口,以及优先级任务队列。主线程首先添加5个普通任务,1s后添加一个高优先级任务,当前3个线程中的最先一个空闲后,会最先执行后面添加的priorityFunc().

std::mutex g_mutex;

void priorityFunc()
{
  for (int i = 1; i < 4; ++i)
  {
      std::this_thread::sleep_for(std::chrono::seconds(1));
      std::lock_guard<std::mutex> lock(g_mutex);
      LOG_DEBUG << "priorityFunc() [" << i << "at thread [ " << CurrentThread::tid() << "] output";// << std::endl;
  }

}

void testFunc()
{
  // loop to print character after a random period of time
  for (int i = 1; i < 4; ++i)
  {
      std::this_thread::sleep_for(std::chrono::seconds(1));
      std::lock_guard<std::mutex> lock(g_mutex);
      LOG_DEBUG << "testFunc() [" << i << "] at thread [ " << CurrentThread::tid() << "] output";// << std::endl;
  }

}


int main()
{
  ThreadPool threadPool;
  threadPool.start();

  for(int i = 0; i < 5 ; i++)
    threadPool.addTask(testFunc);

  std::this_thread::sleep_for(std::chrono::seconds(1));

  threadPool.addTask(ThreadPool::TaskPair(ThreadPool::level0, priorityFunc));

  getchar();
  return 0;
}

./test.out 
2018-11-25 18:24:20.886837 [TRACE] [ThreadPool.cpp:56] [threadLoop] ThreadPool::threadLoop() tid : 4121 start.
2018-11-25 18:24:20.886893 [TRACE] [ThreadPool.cpp:103] [take] ThreadPool::take() tid : 4121 wakeup.
2018-11-25 18:24:20.887580 [TRACE] [ThreadPool.cpp:56] [threadLoop] ThreadPool::threadLoop() tid : 4120 start.
2018-11-25 18:24:20.887606 [TRACE] [ThreadPool.cpp:103] [take] ThreadPool::take() tid : 4120 wakeup.
2018-11-25 18:24:20.887610 [TRACE] [ThreadPool.cpp:56] [threadLoop] ThreadPool::threadLoop() tid : 4122 start.
2018-11-25 18:24:20.887620 [TRACE] [ThreadPool.cpp:103] [take] ThreadPool::take() tid : 4122 wakeup.
2018-11-25 18:24:21.887779 [DEBUG] [main.cpp:104] [testFunc] testFunc() [1] at thread [ 4120] output
2018-11-25 18:24:21.887813 [DEBUG] [main.cpp:104] [testFunc] testFunc() [1] at thread [ 4122] output
2018-11-25 18:24:21.888909 [DEBUG] [main.cpp:104] [testFunc] testFunc() [1] at thread [ 4121] output
2018-11-25 18:24:22.888049 [DEBUG] [main.cpp:104] [testFunc] testFunc() [2] at thread [ 4120] output
2018-11-25 18:24:22.888288 [DEBUG] [main.cpp:104] [testFunc] testFunc() [2] at thread [ 4122] output
2018-11-25 18:24:22.889978 [DEBUG] [main.cpp:104] [testFunc] testFunc() [2] at thread [ 4121] output
2018-11-25 18:24:23.888467 [DEBUG] [main.cpp:104] [testFunc] testFunc() [3] at thread [ 4120] output
2018-11-25 18:24:23.888724 [TRACE] [ThreadPool.cpp:103] [take] ThreadPool::take() tid : 4120 wakeup.
2018-11-25 18:24:23.888778 [DEBUG] [main.cpp:104] [testFunc] testFunc() [3] at thread [ 4122] output
2018-11-25 18:24:23.888806 [TRACE] [ThreadPool.cpp:103] [take] ThreadPool::take() tid : 4122 wakeup.
2018-11-25 18:24:23.890413 [DEBUG] [main.cpp:104] [testFunc] testFunc() [3] at thread [ 4121] output
2018-11-25 18:24:23.890437 [TRACE] [ThreadPool.cpp:103] [take] ThreadPool::take() tid : 4121 wakeup.
2018-11-25 18:24:24.889247 [DEBUG] [main.cpp:92] [priorityFunc] priorityFunc() [1at thread [ 4120] output
2018-11-25 18:24:24.891187 [DEBUG] [main.cpp:104] [testFunc] testFunc() [1] at thread [ 4121] output
2018-11-25 18:24:24.893163 [DEBUG] [main.cpp:104] [testFunc] testFunc() [1] at thread [ 4122] output
2018-11-25 18:24:25.889567 [DEBUG] [main.cpp:92] [priorityFunc] priorityFunc() [2at thread [ 4120] output
2018-11-25 18:24:25.891477 [DEBUG] [main.cpp:104] [testFunc] testFunc() [2] at thread [ 4121] output
2018-11-25 18:24:25.893450 [DEBUG] [main.cpp:104] [testFunc] testFunc() [2] at thread [ 4122] output
2018-11-25 18:24:26.890295 [DEBUG] [main.cpp:92] [priorityFunc] priorityFunc() [3at thread [ 4120] output
2018-11-25 18:24:26.890335 [TRACE] [ThreadPool.cpp:99] [take] ThreadPool::take() tid : 4120 wait.
2018-11-25 18:24:26.892265 [DEBUG] [main.cpp:104] [testFunc] testFunc() [3] at thread [ 4121] output
2018-11-25 18:24:26.892294 [TRACE] [ThreadPool.cpp:99] [take] ThreadPool::take() tid : 4121 wait.
2018-11-25 18:24:26.894274 [DEBUG] [main.cpp:104] [testFunc] testFunc() [3] at thread [ 4122] output
2018-11-25 18:24:26.894299 [TRACE] [ThreadPool.cpp:99] [take] ThreadPool::take() tid : 4122 wait.

2018-11-25 18:24:35.359003 [TRACE] [ThreadPool.cpp:37] [stop] ThreadPool::stop() stop.
2018-11-25 18:24:35.359043 [TRACE] [ThreadPool.cpp:42] [stop] ThreadPool::stop() notifyAll().
2018-11-25 18:24:35.359061 [TRACE] [ThreadPool.cpp:103] [take] ThreadPool::take() tid : 4120 wakeup.
2018-11-25 18:24:35.359067 [TRACE] [ThreadPool.cpp:65] [threadLoop] ThreadPool::threadLoop() tid : 4120 exit.
2018-11-25 18:24:35.359080 [TRACE] [ThreadPool.cpp:103] [take] ThreadPool::take() tid : 4122 wakeup.
2018-11-25 18:24:35.359090 [TRACE] [ThreadPool.cpp:65] [threadLoop] ThreadPool::threadLoop() tid : 4122 exit.
2018-11-25 18:24:35.359123 [TRACE] [ThreadPool.cpp:103] [take] ThreadPool::take() tid : 4121 wakeup.
2018-11-25 18:24:35.359130 [TRACE] [ThreadPool.cpp:65] [threadLoop] ThreadPool::threadLoop() tid : 4121 exit.

3. 课程的线程池 

// project4.cpp: 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <iostream>
#include <vector>
#include <map>
#include <string>
#include <thread>
#include <list>
#include <mutex>
#include <future>

using namespace std;

class A {
public:
	atomic<int> atm;
	A()
	{
		atm = 0;
		//auto atm2 = atm; //这种定义时初始化操作不允许,显示 “尝试引用已删除的函数”编译器内部肯定把拷贝构造函数给干掉了用 = delete
		//atomic<int> atm3 = atm;
		//atomic<int> atm2;
		//atm2 = atm; //尝试引用已删除的函数,拷贝赋值运算符也不让用
		//load():以原子方式读atomic对象的值
		atomic<int> atm2(atm.load()); //读
		auto atm3(atm.load());
		//store()以原子方式写入内容
		atm2.store(12);
		atm2 = 12;

	}

	void outMsgRecvQueue()
	{
		//int command = 0;
		//while (true)
		//{
		//	std::unique_lock<std::mutex> sbguard1(my_mutex1); //临界进去
		//	my_cond.wait(sbguard1, [this] {  //this,可以参考 未归类知识点,第八节
		//		if (!msgRecvQueue.empty())
		//			return true; // 该lambda返回true,则wait就返回,流程走下来,互斥锁被本线程拿到。
		//		return false; //解锁并休眠,卡在wait等待被再次唤醒
		//	});
		//	//现在互斥锁锁着,流程走下来了,队列里有数据;
		//	command = msgRecvQueue.front(); //返回第一个元素,但不检查元素是否存在;
		//	msgRecvQueue.pop_front();  //移除第一个元素,但不返回;
		//	sbguard1.unlock(); //因为unique_lock的灵活性,我们可以随时unlock解锁,以免锁住太长时间
		//	cout << "outMsgRecvQueue()执行,取出一个元素" << command << endl;
		//} //end while
		while (true)
		{
			cout << atm << endl; //读atm是个原子操作,但是整个这一行代码并不是个原子操作;
		}
	}

	void inMsgRecvQueue() //unlock()
	{
		//for (int i = 0; i < 100000; ++i)
		//{
		//	cout << "inMsgRecvQueue()执行,插入一个元素" << i << endl;
		//	std::unique_lock<std::mutex> sbguard1(my_mutex1);
		//	msgRecvQueue.push_back(i); //假设这个数字i就是我收到的命令,我直接弄到消息队列里边来;
		//	my_cond.notify_one();   //我们尝试把wait()的线程唤醒,其实现在outMsgRecvQueue()中的my_cond.wait()已经醒了,但光醒了没有用,你这里要是不把互斥量撒开,醒了他也要堵在另外一个线程的wait()那里;
		//}
		for (int i = 0; i < 1000000; ++i)
		{
			atm += 1; //原子操作
			//atm = atm + 1; //不是原子操作
		}
		return;
	}

private:
	std::list<int> msgRecvQueue; //容器(消息队列),专门用于代表玩家给咱们发送过来的命令。
	std::mutex my_mutex1; //创建了一个互斥量 (一把锁头)	
	std::condition_variable my_cond; //生成一个条件对象

};

int main()
{
	//一:补充一些知识点
	//(1.1)虚假唤醒:wait中要有第二参数(lambda)并且这个lambda中要正确判断要处理的公共数据是否存在;
	//wait(),notify_one(),notify_all()

	//(1.2)atomic ,10,11节都有介绍

	//二:浅谈线程池
	//(2.1)场景设想
	//服务器程序,--》客户端, 每来 一个客户端,就创建 一个新线程为该客户提供服务。
	//a)网络游戏,2万玩家不可能给每个玩家创建个新线程,此程序写法在这种场景下不通;
	//b)程序稳定性问题:编写的代码中,偶尔创建一个线程这种代码,这种写法,就让人感到不安;
	//线程池:把一堆线程弄到一起,统一管理。这种统一管理调度,循环利用线程的方式,就叫线程池;
	//(2.2)实现方式
	//在程序启时,我一次性的创建好一定数量的线程。10,8,100-200,更让人放心,觉得程序代码更稳定;

	//三:线程创建数量谈
	//(3.1)线程开的数量极限问题,2000个线程基本就是极限;再创建线程就崩溃;
	//(3.2)线程创建数量建议
	//a)采用某些技术开发程序;api接口提供商建议你 创建线程数量 = cpu数量,cpu *2 ,cpu *2 +2,遵照专业建议和指示来,专业意见确保程序高效率执行
    //b)创建多线程完成业务; 一个线程等于一条执行通路; 100要堵塞充值,我们这里开110个线程,那是很合适的;
	//c)1800个线程,建议,线程数量尽量不要超过500个,能控制在200个之内;


	//四:c++11多线程总结
	//windows,linux;



	A myobja;
	std::thread myOutnMsgObj(&A::outMsgRecvQueue, &myobja); //第二个参数是 引用,才能保证线程里 用的是同一个对象。
	std::thread myInMsgObj(&A::inMsgRecvQueue, &myobja);
	std::thread myInMsgObj2(&A::inMsgRecvQueue, &myobja);
	myInMsgObj.join();
	myOutnMsgObj.join();
	myInMsgObj2.join();




	//int abc = 10'00; c++新标准允许这么写
	return 0;
}
  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
线程池是一种常见的并发编程技术,它可以有效地管理线程,提高程序的并发性能。在 C 语言中,我们可以使用 POSIX 线程库来实现线程池。 以下是一个简单的 C 语言线程池实现: ```c #include <stdio.h> #include <stdlib.h> #include <pthread.h> #define MAX_THREADS 10 #define MAX_QUEUE 1000 typedef struct { void (*function)(void *); void *argument; } task_t; static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER; static pthread_cond_t notify = PTHREAD_COND_INITIALIZER; static task_t task_queue[MAX_QUEUE]; static int queue_head = 0, queue_tail = 0, queue_size = 0; static int shutdown = 0; static pthread_t thread_pool[MAX_THREADS]; static int thread_count = 0; static void *thread_function(void *arg) { while (1) { pthread_mutex_lock(&lock); while (queue_size == 0 && !shutdown) { pthread_cond_wait(&notify, &lock); } if (shutdown) { pthread_mutex_unlock(&lock); pthread_exit(NULL); } void (*task_function)(void *); void *task_argument; task_function = task_queue[queue_head].function; task_argument = task_queue[queue_head].argument; queue_size--; queue_head++; if (queue_head == MAX_QUEUE) { queue_head = 0; } pthread_mutex_unlock(&lock); (*task_function)(task_argument); } return NULL; } int thread_pool_init(int num_threads) { if (num_threads <= 0 || num_threads > MAX_THREADS) { num_threads = MAX_THREADS; } for (int i = 0; i < num_threads; i++) { if (pthread_create(&thread_pool[i], NULL, thread_function, NULL) != 0) { return -1; } thread_count++; } return 0; } int thread_pool_add_task(void (*function)(void *), void *argument) { pthread_mutex_lock(&lock); if (queue_size == MAX_QUEUE) { pthread_mutex_unlock(&lock); return -1; } task_queue[queue_tail].function = function; task_queue[queue_tail].argument = argument; queue_size++; queue_tail++; if (queue_tail == MAX_QUEUE) { queue_tail = 0; } pthread_cond_signal(&notify); pthread_mutex_unlock(&lock); return 0; } int thread_pool_destroy() { pthread_mutex_lock(&lock); shutdown = 1; pthread_cond_broadcast(&notify); pthread_mutex_unlock(&lock); for (int i = 0; i < thread_count; i++) { pthread_join(thread_pool[i], NULL); } return 0; } ``` 使用方法如下: ```c void print_message(void *arg) { char *message = (char *) arg; printf("%s\n", message); free(message); } int main() { thread_pool_init(4); for (int i = 0; i < 8; i++) { char *message = malloc(sizeof(char) * 20); sprintf(message, "Task %d", i); thread_pool_add_task(print_message, message); } thread_pool_destroy(); return 0; } ``` 该示例代码中,我们定义了一个任务结构体 `task_t`,包含了任务函数指针和参数。线程池中维护了一个任务队列,当有任务添加时,将任务加入队列,等待线程池中的线程来执行。线程池的每个线程都会从任务队列中取出一个任务并执行。线程池的销毁通过发送停止信号来实现,即将 `shutdown` 标志设置为 1,并唤醒所有等待条件变量的线程。最后,等待所有线程执行结束,然后退出线程池

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值