C++开源线程池WThreadPool

gitee地址:WThreadPool: 基于C++语言编写的线程池

介绍

基于C++语言编写的线程池,暂时采用有锁队列,有管理线程,在任务繁忙时会增加线程,在任务少时会减少线程以节省资源.该工程是跨平台的

软件架构

软件架构说明

安装教程

直接使用源码中LockQueue.hpp、WThreadPool.h、WThreadPool.cpp文件集成到自己的工程即可

使用说明

1、void setMaxThreadNum(int maxNum),设置最大线程数

2、bool waitForDone(int waitMs),阻塞等待所以任务完成,可传入等待超时时间,若传入负值,则永久等待

3、void concurrentRun(Func func, Arguments... args),最关键的函数,传入任务函数,会在子线程中执行。该接口与std::thread是一致的,支持lambda表达式

示例:

int main() {

  WThreadPool threadPool;

  threadPool.setMaxThreadNum(10);

  cout << "the main thread  pid: " << this_thread::get_id() << endl;
  for (int i = 0; i < 20; i++)
  {
     threadPool.concurrentRun([=] (int index) {
     cout << "the sub thread  pid: " << this_thread::get_id() << " ,index is "         << index << endl;
    }, i);
  }

  bool flag = threadPool.waitForDone(-1);
  cout << "get here to wait: " << flag << endl;
  return 0;

}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值