All Projects → diharaw → dw-thread-pool

diharaw / dw-thread-pool

Licence: MIT license
A simple, header-only, dependency-free, C++ 11 based ThreadPool library.

Programming Languages

C++
36643 projects - #6 most used programming language
CMake
9771 projects

Projects that are alternatives of or similar to dw-thread-pool

TaskManager
A C++14 Task Manager / Scheduler
Stars: ✭ 81 (+211.54%)
Mutual labels:  thread-pool, task-scheduler
Marl
A hybrid thread / fiber task scheduler written in C++ 11
Stars: ✭ 1,078 (+4046.15%)
Mutual labels:  thread-pool, task-scheduler
php-task
Interface library for the PHPTask library
Stars: ✭ 23 (-11.54%)
Mutual labels:  task-scheduler
goethe
Threading and Caching Utilities for golang
Stars: ✭ 30 (+15.38%)
Mutual labels:  thread-pool
LFTPool
Lock-Free Thread Pool
Stars: ✭ 69 (+165.38%)
Mutual labels:  thread-pool
theeye-of-sauron
TheEye Dockers and QuickStart
Stars: ✭ 27 (+3.85%)
Mutual labels:  task-scheduler
gohive
🐝 A Highly Performant and easy to use goroutine pool for Go
Stars: ✭ 41 (+57.69%)
Mutual labels:  task-scheduler
not-enough-standards
A modern header-only C++ library that provides platform-independent utilities.
Stars: ✭ 197 (+657.69%)
Mutual labels:  thread-pool
thread-pool
A modern thread pool implementation based on C++20
Stars: ✭ 104 (+300%)
Mutual labels:  thread-pool
hicma
HiCMA: Hierarchical Computations on Manycore Architectures
Stars: ✭ 21 (-19.23%)
Mutual labels:  task-scheduler
java-sdk
一些常用的java sdk和工具类(日期工具类,分布式锁,redis缓存,二叉树,反射工具类,线程池,对称/非对称/分段加解密,json序列化,http工具,雪花算法,字符串相似度,集合操作工具,xml解析,重试Retry工具类,Jvm监控等)
Stars: ✭ 26 (+0%)
Mutual labels:  thread-pool
orkid-node
Reliable and modern Redis Streams based task queue for Node.js 🤖
Stars: ✭ 61 (+134.62%)
Mutual labels:  task-scheduler
FilterTreeView
WPF/MVVM Search and Filter Reference Application
Stars: ✭ 56 (+115.38%)
Mutual labels:  task-scheduler
akali
C++ Common Library for Windows, Linux.
Stars: ✭ 34 (+30.77%)
Mutual labels:  thread-pool
haxe-concurrent
A haxelib for basic platform-agnostic concurrency support
Stars: ✭ 69 (+165.38%)
Mutual labels:  thread-pool
penguinV
Simple and fast C++ image processing library with focus on heterogeneous systems
Stars: ✭ 110 (+323.08%)
Mutual labels:  thread-pool
util
封装了一些Java常用的功能
Stars: ✭ 19 (-26.92%)
Mutual labels:  thread-pool
gronx
Lightweight, fast and dependency-free Cron expression parser (due checker), task scheduler and/or daemon for Golang (tested on v1.13 and above) and standalone usage
Stars: ✭ 206 (+692.31%)
Mutual labels:  task-scheduler
Mgx
🌈 A high performance network framework written in c++ (support tcp and http)
Stars: ✭ 15 (-42.31%)
Mutual labels:  thread-pool
crontab
cron expression parser and executor for dotnet core.
Stars: ✭ 13 (-50%)
Mutual labels:  task-scheduler

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.
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].