Giter VIP home page Giter VIP 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

flandr avatar alex-maginatics avatar

Watchers

James Cloos avatar Qing Wang 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.