All Projects → DomGries → ThreadPool2

DomGries / ThreadPool2

Licence: Unlicense license
Lightweight, Generic, Pure C++11 ThreadPool

Projects that are alternatives of or similar to ThreadPool2

Flexml
🚀基于Litho的Android高性能动态业务容器。
Stars: ✭ 225 (+703.57%)
Mutual labels:  lightweight
Task Easy
A simple, customizable, and lightweight priority queue for promises.
Stars: ✭ 244 (+771.43%)
Mutual labels:  lightweight
Api
Minimal, extremely fast, lightweight Ruby framework for HTTP APIs
Stars: ✭ 252 (+800%)
Mutual labels:  lightweight
Mu
A tweet-sized PHP micro-router
Stars: ✭ 229 (+717.86%)
Mutual labels:  lightweight
Libuwsc
A Lightweight and fully asynchronous WebSocket client library based on libev
Stars: ✭ 237 (+746.43%)
Mutual labels:  lightweight
Vpp
Modern C++ vulkan utility library.
Stars: ✭ 245 (+775%)
Mutual labels:  lightweight
Libonnx
A lightweight, portable pure C99 onnx inference engine for embedded devices with hardware acceleration support.
Stars: ✭ 217 (+675%)
Mutual labels:  lightweight
Moveto
A lightweight scroll animation javascript library without any dependency
Stars: ✭ 2,746 (+9707.14%)
Mutual labels:  lightweight
Quick Picture Viewer
🖼️ Lightweight, versatile desktop image viewer for Windows. The best replacement for the default Windows photo viewer.
Stars: ✭ 237 (+746.43%)
Mutual labels:  lightweight
Yay Evil Emacs
😈 A lightweight literate Emacs config with even better "better defaults". Shipped with a custom theme!
Stars: ✭ 250 (+792.86%)
Mutual labels:  lightweight
Imdn
Lightweight Image Super-Resolution with Information Multi-distillation Network (ACM MM 2019, Winner Award of ICCVW AIM 2019 Constrained SR Track1&Track2)
Stars: ✭ 229 (+717.86%)
Mutual labels:  lightweight
Herbe
Daemon-less notifications without D-Bus. Minimal and lightweight.
Stars: ✭ 235 (+739.29%)
Mutual labels:  lightweight
Gunslinger
C99, header-only framework for games and multimedia applications
Stars: ✭ 246 (+778.57%)
Mutual labels:  lightweight
Snackbar
A tiny browser library for showing a brief message at the bottom of the screen (1kB gzipped).
Stars: ✭ 224 (+700%)
Mutual labels:  lightweight
Yalla
YallaJS, ES6 Templating Engine.
Stars: ✭ 253 (+803.57%)
Mutual labels:  lightweight
Textosaurus
Cross-platform text editor based on Qt and Scintilla.
Stars: ✭ 224 (+700%)
Mutual labels:  lightweight
Libaco
A blazing fast and lightweight C asymmetric coroutine library 💎 ⛅🚀⛅🌞
Stars: ✭ 2,918 (+10321.43%)
Mutual labels:  lightweight
simple-jwt-provider
No description or website provided.
Stars: ✭ 33 (+17.86%)
Mutual labels:  lightweight
Pareto.js
An extremely small, intuitive and fast functional utility library for JavaScript
Stars: ✭ 254 (+807.14%)
Mutual labels:  lightweight
Pine
A modern, native macOS markdown editor
Stars: ✭ 2,818 (+9964.29%)
Mutual labels:  lightweight

ThreadPool

Lightweight, Generic, Pure C++11 ThreadPool

Licensing

Public Domain. Use at your own risk for whatever you want. If your country doesn't have a public domain, feel free to say you found this on the side of the road.

Overview

ThreadPool is a super simple class that manages threads and jobs. threadCount threads are created at object instantiation time, and persist until the ThreadPool object is destroyed. You cannot change the thread count. Jobs are functions with no parameters or return values. This decision was to make it as generic as possible so it could be integrated into a variety of projects. If you can't get your job to work with those constraints, you're doing something wrong, or you need to roll your own ThreadPool. But you're probably making things overly complicated.

Below is a quick overview, but ThreadPool.h is documented, so just read that. It's less than 200 lines with comments.

class ThreadPool {
public:
    ThreadPool(int threadCount);
    ~ThreadPool();
    void AddJob(std::function<void(void)> job);
    void JoinAll();
    void WaitAll();
};

Examples

#include "ThreadPool.h"

#include <iostream>
#include <chrono>

int main() {
    ThreadPool pool(10); // Creates 10 threads.
    int JOB_COUNT = 100;
    
    for (int i = 0; i < JOB_COUNT; ++i)
        pool.AddJob([]() { 
            std::this_thread::sleep_for(std::chrono::seconds(1));
        });
    
    pool.JoinAll();
    std::cout << "Expected runtime: 10 seconds." << std::endl;
}

Convience Function for running a list of jobs in a pool, assuming the type being iterated is of std::function<void(void)>:

template <typename Iter>
void RunInPool(Iter begin, Iter end, int threadCount) {
    ThreadPool pool(threadCount);
    for( ; begin != end; begin = std::next(begin))
        pool.AddJob(*begin);
    pool.JoinAll();
}

It's worth nothing that pool.JoinAll(); is optional in this example, since JoinAll is invoked upon object deconstruction.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].