Coder Social home page Coder Social logo

tp4cpp's Introduction

tp4cpp - A threadpool implemented for C++

1. Structure

  • Thread : the wrapper class of pthread for easily usage
  • Task : abstract base class for user-defined task
  • ThreadPool : the main thread pool class

2. Usage

Define the concrete task by deriving a sub-class from Task

class TestTask : public tp_ns::Task {
public:
    int run(void *arg) override
    {
        for (int i = 0; i < 10; ++i) {
            std::cout << i << '\t';
        }
        std::cout << std::endl;
        return 0;
    }
};

class TestTask1 : public tp_ns::Task {
public:
    int run(void *arg) override
    {
        int *a = reinterpret_cast<int*>(arg);
        std::cout << "input args: " << *a << std::endl;
        return 0;
    }
};

Init the ThreadPool object, add worker to the pool and calling run

    tp_ns::ThreadPool pool;
    pool.add_task(&tt); //`tt` should be definied before define `pool`

    //use `new` and the pool will free them
    int arg = 11111;
    pool.add_task(new TestTask1(), (void*)&arg, true); 

    pool.run();

The ThreadPool can free the new Task by given the third argument of the add_task method. You can also add the local task object which should defined before the ThreadPool object.


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.