Convert Figma logo to code with AI

progschj logoThreadPool

A simple C++11 Thread Pool implementation

7,820
2,233
7,820
56

Top Related Projects

1,785

Modern and efficient C++ Thread Pool Library

A fast multi-producer, multi-consumer lock-free concurrent queue for C++11

5,580

oneAPI Threading Building Blocks (oneTBB)

10,061

A General-purpose Task-parallel Programming System using Modern C++

BS::thread_pool: a fast, lightweight, and easy-to-use C++17 thread pool library

Quick Overview

ThreadPool is a simple C++11 thread pool implementation. It allows users to easily create a pool of worker threads and queue tasks for execution, providing a convenient way to manage concurrent operations in C++ applications.

Pros

  • Easy to use and integrate into existing C++ projects
  • Header-only library, requiring no additional compilation steps
  • Supports both function pointers and lambda expressions for task submission
  • Automatically manages thread creation, destruction, and task distribution

Cons

  • Limited features compared to more comprehensive threading libraries
  • No built-in support for task prioritization or cancellation
  • May not be suitable for highly complex or specialized threading scenarios
  • Limited documentation and examples available in the repository

Code Examples

  1. Creating a thread pool and submitting a simple task:
#include "ThreadPool.h"
#include <iostream>

int main() {
    ThreadPool pool(4);  // Create a thread pool with 4 worker threads
    
    auto result = pool.enqueue([](int number) {
        return number * 2;
    }, 42);
    
    std::cout << "Result: " << result.get() << std::endl;
    return 0;
}
  1. Submitting multiple tasks and collecting results:
#include "ThreadPool.h"
#include <vector>
#include <iostream>

int main() {
    ThreadPool pool(4);
    std::vector<std::future<int>> results;

    for(int i = 0; i < 8; ++i) {
        results.emplace_back(
            pool.enqueue([i] {
                return i * i;
            })
        );
    }

    for(auto && result: results) {
        std::cout << result.get() << ' ';
    }
    std::cout << std::endl;
    return 0;
}
  1. Using the thread pool with a member function:
#include "ThreadPool.h"
#include <iostream>

class MyClass {
public:
    int multiply(int a, int b) { return a * b; }
};

int main() {
    ThreadPool pool(4);
    MyClass obj;

    auto result = pool.enqueue(&MyClass::multiply, &obj, 6, 7);
    std::cout << "Result: " << result.get() << std::endl;
    return 0;
}

Getting Started

To use ThreadPool in your project:

  1. Download the ThreadPool.h file from the repository.
  2. Include the header in your C++ file: #include "ThreadPool.h"
  3. Create a ThreadPool object with the desired number of threads: ThreadPool pool(4);
  4. Submit tasks using the enqueue method: auto result = pool.enqueue([]() { /* your task */ });
  5. Retrieve results using the returned std::future object: result.get();

Make sure to compile your project with C++11 support (e.g., using -std=c++11 flag with GCC or Clang).

Competitor Comparisons

1,785

Modern and efficient C++ Thread Pool Library

Pros of CTPL

  • More feature-rich, offering priority queue and the ability to change the number of threads at runtime
  • Better exception handling and propagation to the caller
  • Supports C++11 and later versions, providing broader compatibility

Cons of CTPL

  • More complex implementation, which may be harder to understand and maintain
  • Slightly higher memory footprint due to additional features
  • May have a small performance overhead compared to the simpler ThreadPool implementation

Code Comparison

ThreadPool:

ThreadPool pool(4);
auto result = pool.enqueue([](int answer) { return answer; }, 42);

CTPL:

ctpl::thread_pool p(4);
auto result = p.push([](int id, int answer) { return answer; }, 42);

Key Differences

  • CTPL passes a thread ID to the task function, which can be useful for debugging
  • ThreadPool uses std::future directly, while CTPL wraps it in a custom future class
  • CTPL allows for more granular control over thread creation and destruction

Use Cases

  • ThreadPool: Simpler projects with straightforward multithreading needs
  • CTPL: More complex applications requiring advanced thread pool features and finer control

A fast multi-producer, multi-consumer lock-free concurrent queue for C++11

Pros of concurrentqueue

  • Specialized for high-performance concurrent queue operations
  • Lock-free implementation for better scalability
  • Supports bulk enqueue and dequeue operations

Cons of concurrentqueue

  • Limited to queue operations, not a general-purpose thread pool
  • May require more complex integration for task scheduling
  • Lacks built-in thread management features

Code Comparison

ThreadPool:

ThreadPool pool(4);
auto result = pool.enqueue([](int answer) { return answer; }, 42);

concurrentqueue:

moodycamel::ConcurrentQueue<int> q;
q.enqueue(42);
int item;
q.try_dequeue(item);

Summary

ThreadPool is a general-purpose thread pool implementation, while concurrentqueue focuses on high-performance concurrent queue operations. ThreadPool provides easier task scheduling and thread management, but concurrentqueue offers better scalability for queue-specific operations. The choice between them depends on the specific requirements of your concurrent programming needs.

5,580

oneAPI Threading Building Blocks (oneTBB)

Pros of oneTBB

  • More comprehensive and feature-rich, offering a wide range of parallel algorithms and data structures
  • Actively maintained by Intel with regular updates and optimizations
  • Supports task-based parallelism and work-stealing for better load balancing

Cons of oneTBB

  • Steeper learning curve due to its extensive API and features
  • Larger library size and potential overhead for simpler use cases
  • May be overkill for projects that only require basic thread pooling

Code Comparison

ThreadPool:

ThreadPool pool(4);
auto result = pool.enqueue([](int answer) { return answer; }, 42);

oneTBB:

tbb::task_group g;
g.run([&] { /* task code */ });
g.wait();

Summary

ThreadPool is a lightweight, easy-to-use thread pool implementation, ideal for simple multithreading tasks. oneTBB, on the other hand, is a comprehensive parallel programming library that offers advanced features and optimizations, suitable for more complex parallel computing scenarios. While ThreadPool is simpler to integrate and use, oneTBB provides better scalability and performance for larger, more demanding applications.

10,061

A General-purpose Task-parallel Programming System using Modern C++

Pros of Taskflow

  • More feature-rich, supporting complex task dependencies and workflows
  • Better performance for large-scale parallel and heterogeneous workloads
  • Actively maintained with regular updates and improvements

Cons of Taskflow

  • Steeper learning curve due to more complex API
  • Larger codebase and potential overhead for simple use cases
  • May be overkill for projects requiring basic thread pooling

Code Comparison

ThreadPool example:

ThreadPool pool(4);
auto result = pool.enqueue([](int answer) { return answer; }, 42);

Taskflow example:

tf::Executor executor;
tf::Taskflow taskflow;
auto [A, B, C, D] = taskflow.emplace(
  []() { std::cout << "Task A\n"; },
  []() { std::cout << "Task B\n"; },
  []() { std::cout << "Task C\n"; },
  []() { std::cout << "Task D\n"; }
);
A.precede(B, C);
B.precede(D);
C.precede(D);
executor.run(taskflow).wait();

Summary

ThreadPool is simpler and more lightweight, ideal for basic thread pooling needs. Taskflow offers advanced features for complex parallel workflows but requires more setup and understanding. Choose based on your project's complexity and performance requirements.

BS::thread_pool: a fast, lightweight, and easy-to-use C++17 thread pool library

Pros of thread-pool

  • More feature-rich, including task prioritization and parallel_for loops
  • Actively maintained with regular updates and improvements
  • Extensive documentation and examples provided

Cons of thread-pool

  • Slightly more complex API due to additional features
  • Requires C++17 or later, limiting compatibility with older codebases

Code Comparison

ThreadPool:

ThreadPool pool(4);
auto result = pool.enqueue([](int answer) { return answer; }, 42);

thread-pool:

thread_pool pool(4);
auto result = pool.submit([](int answer) { return answer; }, 42);

Key Differences

  • ThreadPool uses enqueue while thread-pool uses submit for task submission
  • thread-pool offers more advanced features like wait_for_tasks() and parallelize_loop()
  • ThreadPool is simpler and may be easier to integrate into existing projects
  • thread-pool provides better performance optimizations and thread management

Both libraries offer efficient thread pooling solutions, but thread-pool is more modern and feature-rich, while ThreadPool is simpler and more lightweight.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

ThreadPool

A simple C++11 Thread Pool implementation.

Basic usage:

// create thread pool with 4 worker threads
ThreadPool pool(4);

// enqueue and store future
auto result = pool.enqueue([](int answer) { return answer; }, 42);

// get result from future
std::cout << result.get() << std::endl;