Code Monkey home page Code Monkey logo

taskqueue's Introduction

TaskQueue

Modern C++ Generic Thread Pool

Compact yet powerful concurrent thread pool implementation. Requires C++17.

Documentation

Functions marked with [Thread Safe] indicate safe concurrent usage.

Class

TaskQueue<T>

Creates a task queue object, T is the type of task that can be queued. T must be a functor.

Constructors

TaskQueue::TaskQueue()

Default constructor, does not deploy any workers.

TaskQueue::TaskQueue(size_t WorkerCount)

Allocates WorkerCount number of threads, which perform a state evaluation and immediately sleep if no work is in the queue.

Functions

int TaskQueue::GetWorkerCount()

[Thread Safe] Returns the number of active workers. This includes sleeping workers, but does not include workers recently killed that are finishing their active task.

void TaskQueue::Resize(size_t NewWorkerCount)

[Thread Safe] Resizes the worker pool to have NewWorkerCount worker threads, either allocating and deploying new workers or killing off some workers, depending on NewWorkerCount. If a worker that is performing a task is killed, they will complete their task before deallocating.

template <typename U>
int Enqueue(U&& Task)

[Thread Safe] Enqueues a new task for a worker to process when available. If no workers are in the pool, the task will sit in queue until a resize. U must match the type T. This extra template is used to leverage reference collapsing, letting Task be an L value or an R value. Returns the ID of the newly enqueued task, -1 if a failure occurred.

int GetUnstartedTasksCount()

[Thread Safe] Returns the number of tasks in the queue that haven't been started by a worker yet.

bool CancelUnstartedTask(int ID)

[Thread Safe] Cancels a task if it hasn't yet been started by a worker. ID is the ID of the target task, which can be obtained by the return value of Enqueue. Returns if the task was successfully cancelled or not.

void CancelAllUnstartedTasks()

[Thread Safe] Cancels all tasks that haven't yet been started by workers.

void Join()

[Thread Safe] Blocks the calling thread until the workers have run all tasks in the queue and have synchronized. Prevents additional work from being enqueued from other threads until this call has completed. Once completed, the task queue is left in an empty working state with no workers.

void Stop()

[Thread Safe] Clears the work queue (Cancels unstarted tasks) and marks all workers for destruction. Kills any and all sleeping workers. Used to cleanly shutdown a task queue. Once completed, the task queue is left in an empty working state with no workers.

taskqueue's People

Contributors

adepke avatar

Stargazers

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

Watchers

 avatar

taskqueue's Issues

Mutex Destroyed While Busy Error

Occasionally, the demo will throw a "mutex destroyed while busy" error during the destruction phase of a task queue's lifetime. Look into this.

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.