DwThreadPool - A simple, header-only, dependency-free, C++ 11 based ThreadPool library.

License: MIT

dwThreadPool

A simple, header-only, dependency-free, C++ 11 based ThreadPool library.

Features

  • C++ 11
  • Minimal Source Code
  • Header-only
  • No external dependencies
  • Task Grouping/Child Tasks
  • Task Continuations
  • No dynamic allocations for the user
  • Fully cross-platform

Compilers

  • MSVC
  • AppleClang

Basics

#include <thread_pool.hpp>

// create simple struct containing task data.
struct my_task_data
{
	  int   foo;
	  float bar;
}

// task functions have to conform to this signature. can be free functions or methods.
void my_task(void* args)
{
	// do work here...
}

int main() 
{
	// create thread pool instance. 
    
    	// Default constructor creates (N-1) number of workers, with N being the number of hardware threads.
    	dw::ThreadPool thread_pool;
    
    	// Allocate a new task from the thread pool. No dynamic allocations are done internally
    	dw::Task* task = thread_pool.allocate();
  
    	// Bind task function.
    	task->function = my_task;
    
    	// Useful template function for casting task data to a pointer of your custom task data struct.
    	my_task_data* data = dw::task_data<my_task_data>(task);
    
    	// Fill in task data.
    	data->foo = 1;
    	data->bar = 2.0f;
    
    	// enqueue task into thread pool.
    	thread_pool.enqueue(task);
    
    	// wait till work is done.
    	thread_pool.wait_for_all();
    
    	return 0;
}

Waiting for a specified Task

dw::Task* task = thread_pool.allocate();

thread_pool.enqueue(task);

// Busy waits on the calling thread until the specified function is done.
thread_pool.wait_for_one(task);

Check whether a specified Task is done

dw::Task* task = thread_pool.allocate();

thread_pool.enqueue(task);

// Returns true is task is done, false if not.
thread_pool.is_done(task);

Task Continuations

dw::Task* task1 = thread_pool.allocate();
dw::Task* task2 = thread_pool.allocate();

// Bind data and functions...

// First, add task2 as a continuation of task1.
thread_pool.add_as_continuation(task1, task2);

// Then enqueue the first task into the thread pool. The second task will automatically run.
thread_pool.enqueue(task1);

Task Grouping/Child Tasks

NOTE: Child Tasks here refer to grouping a set of tasks to finish together. It is not meant to express dependencies between tasks. For that, use Task Continuations.

dw::Task* parent_task;
dw::Task* child_tasks[10];

// Allocate and bind parent task.
parent_task = thread_pool.allocate();

for(uint32_t i = 0; i < 10; i++)
{
	// Allocate and bind Child tasks.
	child_tasks[i] = tp.allocate();

	// Add child as children of the parent task.
	thread_pool.add_as_child(parent_task, child_tasks[i]);

    	// Immediately enqueue each child task.
	thread_pool.enqueue(child_tasks[i]);
}

// Lastly enqueue parent task
thread_pool.enqueue(parent_task);

// Wait on the parent task to ensure the entire task group is completed
thread_pool.wait_for_one(parent_task);

Remotery Screenshot of Example

alt text

Building the Example

The example project can be built using the CMake build system generator. Plenty of tutorials around for that.

License

Copyright (c) 2019 Dihara Wijetunga

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and 
associated documentation files (the "Software"), to deal in the Software without restriction, 
including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, 
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial
portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT 
LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Owner
Dihara Wijetunga
Graphics programmer focusing on high-performance real-time rendering and ray tracing.
Dihara Wijetunga
Similar Resources

A header-only C++ library for task concurrency

A header-only C++ library for task concurrency

transwarp Doxygen documentation transwarp is a header-only C++ library for task concurrency. It allows you to easily create a graph of tasks where eve

Dec 19, 2022

C++20's jthread for C++11 and later in a single-file header-only library

C++20's jthread for C++11 and later in a single-file header-only library

jthread lite: C++20's jthread for C++11 and later A work in its infancy. Suggested by Peter Featherstone. Contents Example usage In a nutshell License

Dec 8, 2022

Header-Only C++20 Coroutines library

CPP20Coroutines Header-Only C++20 Coroutines library This repository aims to demonstrate the capabilities of C++20 coroutines. generator Generates val

Aug 15, 2022

Header-only library for multithreaded programming

CsLibGuarded Introduction The CsLibGuarded library is a standalone header only library for multithreaded programming. This library provides templated

Jan 2, 2023

Portable header-only C++ low level SIMD library

libsimdpp libsimdpp is a portable header-only zero-overhead C++ low level SIMD library. The library presents a single interface over SIMD instruction

Dec 13, 2022

Header-only library for multithreaded programming

CsLibGuarded Introduction The CsLibGuarded library is a standalone header only library for multithreaded programming. This library provides templated

Dec 20, 2022

Cpp-mempool - C++ header-only mempool library

cpp-mempool C++ header-only mempool library

Jun 21, 2022

Simple and fast C library implementing a thread-safe API to manage hash-tables, linked lists, lock-free ring buffers and queues

libhl C library implementing a set of APIs to efficiently manage some basic data structures such as : hashtables, linked lists, queues, trees, ringbuf

Dec 3, 2022

Fiber - A header only cross platform wrapper of fiber API.

Fiber Header only cross platform wrapper of fiber API A fiber is a particularly lightweight thread of execution. Which is useful for implementing coro

Jul 31, 2022
EOSP ThreadPool is a header-only templated thread pool writtent in c++17.

EOSP Threadpool Description EOSP ThreadPool is a header-only templated thread pool writtent in c++17. It is designed to be easy to use while being abl

Apr 22, 2022
ThreadPool - A simple C++11 Thread Pool implementation

ThreadPool A simple C++11 Thread Pool implementation. Basic usage: // create thread pool with 4 worker threads ThreadPool pool(4); // enqueue and sto

Jan 7, 2023
Smart queue that executes tasks in threadpool-like manner

execq execq is kind of task-based approach of processing data using threadpool idea with extended features. It supports different task sources and mai

Dec 22, 2022
Light, fast, threadpool for C++20

riften::Thiefpool A blazing-fast, lightweight, work-stealing thread-pool for C++20. Built on the lock-free concurrent riften::Deque. Usage #include "r

Dec 28, 2022
ThreadPool - A fastest, exception-safety and pure C++17 thread pool.

Warnings Since commit 468129863ec65c0b4ede02e8581bea682351a6d2, I move ThreadPool to C++17. (To use std::apply.) In addition, the rule of passing para

Dec 28, 2022
Threadpool c++17 - 采用多线程多对列,每个线程控制一个队列,替代老的多个线程公用一个队列

ThreadPool c++17 采用多线程多对列,每个线程控制一个队列,替代老的多个线程公用一个队列。 将任务拆分多个下发给每个线程,每个线程掌管 M(tasks) / N(threads)个任务 M(tasks) / N(threads)个任务 公用一个队列。减少竞争。 使用方法: 初始化线程池

Apr 25, 2022
A bounded single-producer single-consumer wait-free and lock-free queue written in C++11
A bounded single-producer single-consumer wait-free and lock-free queue written in C++11

SPSCQueue.h A single producer single consumer wait-free and lock-free fixed size queue written in C++11. Example SPSCQueue<int> q(2); auto t = std::th

Dec 27, 2022
Forkpool - A bleeding-edge, lock-free, wait-free, continuation-stealing scheduler for C++20

riften::Forkpool A bleeding-edge, lock-free, wait-free, continuation-stealing scheduler for C++20. This project uses C++20's coroutines to implement c

Dec 31, 2022
Awesome-lockfree - A collection of resources on wait-free and lock-free programming

Awesome Lock-Free A collection of resources on wait-free and lock-free programming. ?? ?? ?? Even better resource from MattPD: C++ links: atomics, loc

Jan 1, 2023
Parallel-util - Simple header-only implementation of "parallel for" and "parallel map" for C++11

parallel-util A single-header implementation of parallel_for, parallel_map, and parallel_exec using C++11. This library is based on multi-threading on

Jun 24, 2022