Giter VIP home page Giter VIP logo

c-lightlythreadpool's Introduction

一个轻量的C++线程池库

c++ STL 并未自带一个线程池库,因此想借此机会写一个线程池模板,以后想用线程池直接导入

优点

  • 使用有限阻塞队列,而不是在线程池里使用互斥锁,性能好
  • 使用管理线程,在任务繁忙时自动扩充线程池,在任务空闲时减小线程池,合理分配资源

使用方法,将lib目录直接复制到项目地址

然后在想使用线程池的块中导入threadpool.hpp即可

先获取线程池实例

auto threadpool = ThreadPool::instance();

在addTask()这个API中直接加入你想添加到线程池中处理的函数即可

#include "lib/threadpool.hpp"
#include <iostream>

void output()
{
    std::cout<<"func output() : null\n";
}

void sum(int a, int b)
{
    std::cout<<"func sum() :"<<a+b<<std::endl;
}

int main()
{
    auto threadpool = ThreadPool::instance();

    threadpool->addTask(output);
    // 用bind将参数打包
    threadpool->addTask(std::bind(&sum, 5 , 8));

    return 0;
}

输出

func output() : null
func sum() :13

c-lightlythreadpool's People

Stargazers

 avatar wustghj avatar

Watchers

wustghj avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.