Giter Site home page Giter Site logo

threadpool's Introduction

Maginatics ThreadPool

ThreadPool is a C++ implementation of the thread pool pattern, allowing arbitrary tasks to be queued for execution by a dedicated pool of worker threads. It is a header-only library and requires Boost with support for version 4 of the Boost.Thread interface. Nathan Rosenblum at Maginatics [email protected] originally wrote ThreadPool.

Overview

ThreadPool implements a bounded thread pool with a minimum core pool size and configurable keep-alive for idle threads; those familiar with the Java ThreadPoolExecutor will find the interface and semantics familiar. At its core is a pool of preallocated worker threads. New tasks are executed immediately if idle workers are available or if new workers can be allocated without exceeding the limit; otherwise, the tasks are added to a FIFO queue for later execution. A fixed core pool of one or more threads is maintained, with idle threads above this limit terminating after a configurable keep-alive period.

Example use

The ThreadPool constructor takes minPoolSize, maxPoolSize, and keepAlive parameters; the following example demonstrates scheduling a large number of tasks for execution on a pool with a maximum thread count of 512 and a 30 second keep-alive timeout:

void someTask();

ThreadPool pool(1, 512, 30000);
for (int i = 0; i < 1E6; ++i) {
    pool.execute([]() { someTask(); });
}
pool.drain();

Task return values can be retrieved using the schedule interface:

bool someNonVoidTask();

ThreadPool pool(1, 512, 30000);
auto result = pool.schedule<bool>([]() -> bool {
            return someNonVoidTask();
        });
printf("Returned %d\n", result.get());

Refer to the documentation for further details.

Generating documentation

Details of the ThreadPool interface are provided in the form of Doxygen-formatted comments in the code. To produce an HTML version of the documentation, execute

./waf configure build

in the project root. This will produce documentation in the ${PROJECT_ROOT}/build/doc directory.

License

Copyright (C) 2012-2013 Maginatics, Inc.

Licensed under the MIT License. The waf build system distributed alongside this software is licensed under the BSD 3-Clause License.

threadpool's People

Contributors

alex-maginatics avatar flandr avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

threadpool's Issues

Change README to state Boost is not a requirement

Currently there is this text:

... requires Boost with support for version 4 of the Boost.Thread interface

However this is not the case, as the standard library is used by default as of 872fc6d. Better wording would be to mention Boost can be used in place of the standard library.

As as user I would find this beneficial looking for a lightweight thread pool that does not tie into Boost (as I was a few days ago) and might not have looked into the library if I read the Boost portion.

Is this tread safe?

@flandr

What I need to do is have two threads. One of them will keep adding tasks in this thread pool and the other thread will just consume the pool.

Also is there a way to keep listening for tasks? So pool.drain() to listen forever instead of exiting when all tasks are processed? As I will keep adding tasks depending on external events.

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.