Git Product home page Git Product logo

easy-thread-pool's Introduction

Easy-Thread-Pool

A easy thread pool based on C++14, which is a Single-task queue.

1.支持的task类型(Supported task types)

  • 普通函数(function name)
  • 匿名函数(lambda)
  • 仿函数(Overloaded () class or struct)
  • 类成员函数(Class member functions)
  • std::function/std::packaged_task

2.线程池使用

2.1 提交任务

std::future<returnType> res = submitTask(funcName, arg1, arg2, ...);

2.2 获取结果

res.get();

3.使用案例

#include <iostream>
#include <chrono>
#include "ThreadPool.h"

int very_time_consuming_task(int a, int b)  //模拟耗时函数
{
    std::this_thread::sleep_for(std::chrono::seconds(1));
    return a + b;
}

int main()
{
    using namespace pooltech;
    ThreadPool thread_pool{12};
    int taskNum = 30;
    std::vector<std::future<int>> results(taskNum);

    std::cout << "start to submit tasks..." << std::endl;
    for (size_t i = 0; i < taskNum; i++)
    {
        results[i] = thread_pool.submitTask(very_time_consuming_task, i, i + 1);
    }
    std::cout << "End submit tasks..." << std::endl;
    std::cout << "Main thread is doing something else..." << std::endl;

    //do something
    std::this_thread::sleep_for(std::chrono::seconds(10));
    std::cout << "Main thread task finished..." << std::endl;

    std::cout << "try to get the thread pool tasks..." << std::endl;
    for (size_t i = 0; i < taskNum; i++)
    {
        std::cout << "result[" << i << "] = " << results[i].get() << std::endl;
    }
    std::cout << "End of getting results" << std::endl;
    
}

easy-thread-pool's People

Contributors

life-studied avatar

Watchers

 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.