Base——ThreadPool类

1.线程池结

在这里插入图片描述

2.成员变量

在这里插入图片描述

  • cond_:通知线程队列中的线程来取任务
  • threads_:线程队列,里面存放的是muduo::Thread
  • queue_:任务队列
  • running_:此线程池是否工作

muduo新版中添加了一些变量,但实现的基本功能没变
在这里插入图片描述

3.测试用例 ThreadPool_test.cc

以muduo-0.9.1版本为例:

#include <muduo/base/ThreadPool.h>
#include <muduo/base/CountDownLatch.h>
#include <muduo/base/CurrentThread.h>

#include <boost/bind.hpp>
#include <stdio.h>

void print()
{
  printf("tid=%d\n", muduo::CurrentThread::tid());
}

void printString(const std::string& str)
{
  printf("tid=%d, str=%s\n", muduo::CurrentThread::tid(), str.c_str());
}

int main()
{
  muduo::ThreadPool pool("MainThreadPool");
  pool.start(5);

  pool.run(print);
  pool.run(print);
  for (int i = 0; i < 100; ++i)
  {
    char buf[32];
    snprintf(buf, sizeof buf, "task %d", i);
    pool.run(boost::bind(printString, std::string(buf)));
  }

  muduo::CountDownLatch latch(1);
  pool.run(boost::bind(&muduo::CountDownLatch::countDown, &latch));
  latch.wait();
  pool.stop();
}

muduo::ThreadPool pool(“MainThreadPool”);
pool.start(5);

  • 首先创建了一个线程池,并创建出了5个工作线程,每个工作线程函数是 runInThread,如下图所示。
    在这里插入图片描述
  • 接着调用 start()函数,创建线程,接下来的流程参考 6.测试用例 Thread_test.cc
    在这里插入图片描述
  • 线程函数 runInThread定义如下:
    在这里插入图片描述
  • 其中 take()函数定义如下:
    在这里插入图片描述

pool.run(print);
pool.run(print);
for (int i = 0; i < 100; ++i)
{
char buf[32];
snprintf(buf, sizeof buf, “task %d”, i);
pool.run(boost::bind(printString, std::string(buf)));
}

  • 向任务队列中添加 2个print()任务,100个printString()任务
  • run()函数定义如下:
    在这里插入图片描述

muduo::CountDownLatch latch(1);
pool.run(boost::bind(&muduo::CountDownLatch::countDown, &latch));
latch.wait();

  • 向任务队列中添加 CountDownLatch()任务,当工作线程执行完此任务时,latch.wait()解除阻塞。CountDownLatch()定义

pool.stop();

  • 线程池停止工作。
    在这里插入图片描述
  • 由于此时running_ = false,所以线程函数 runInThread会跳出while循环,线程函数结束。
  • 注:muduo新版中添加了多个测试用例,总体实现一样。
  • 运行部分结果:
    在这里插入图片描述
    参考代码
以下是使用C#编写的线程池代码示例: ```csharp using System; using System.Threading; namespace ThreadPoolExample { class Program { static void Main(string[] args) { // 创建线程池对象 ThreadPoolExample threadPoolExample = new ThreadPoolExample(); // 启动线程池 threadPoolExample.Start(); Console.ReadKey(); } } public class ThreadPoolExample { private int _numberOfThreads = 4; // 线程池中的线程数 private int _taskCount = 10; // 要执行的任务数 public void Start() { // 创建线程池 ThreadPool.SetMaxThreads(_numberOfThreads, _numberOfThreads); ThreadPool.SetMinThreads(2, 2); // 创建任务 for (int i = 0; i < _taskCount; i++) { ThreadPool.QueueUserWorkItem(new WaitCallback(ExecuteTask), i); } } private void ExecuteTask(object task) { // 模拟任务执行 Console.WriteLine($"Task {task} is executing on thread {Thread.CurrentThread.ManagedThreadId}"); Thread.Sleep(1000); Console.WriteLine($"Task {task} execution completed on thread {Thread.CurrentThread.ManagedThreadId}"); } } } ``` 在上面的示例中,我们创建了一个 `ThreadPoolExample` ,其中包括一个 `Start()` 方法,该方法使用 `ThreadPool` 来启动线程池并执行任务。 我们在 `Start()` 方法中设置了线程池的最大和最小线程数。然后我们创建了 `_taskCount` 个任务,并将它们添加到线程池中。每个任务都是通过调用 `ThreadPool.QueueUserWorkItem()` 方法来添加的。该方法需要一个 `WaitCallback` 委托,该委托指向要执行的方法。在这里,我们将 `ExecuteTask()` 方法作为要执行的方法。 `ExecuteTask()` 方法是模拟要执行的任务。我们将每个任务的执行延迟 1 秒钟,并在控制台中打印出任务执行的线程 ID。 最后,我们在 `Main()` 方法中实例化了 `ThreadPoolExample` 并调用了 `Start()` 方法来启动线程池。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值