threadpool
based on C++11 , a mini threadpool , accept variable number of parameters 基于C++11的线程池,简洁且可以带任意多的参数
Top Related Projects
Modern and efficient C++ Thread Pool Library
Thread pool implementation using c++11 threads
Quick Overview
The lzpong/threadpool repository is a C++11 thread pool implementation. It provides a simple and efficient way to manage and execute tasks concurrently using a fixed number of worker threads, making it easier to parallelize workloads in C++ applications.
Pros
- Easy to use and integrate into existing C++ projects
- Supports both function pointers and lambda expressions for task submission
- Allows retrieval of task results using std::future
- Lightweight implementation with minimal dependencies
Cons
- Limited documentation and examples
- No built-in support for task prioritization
- Lacks advanced features like thread affinity or work-stealing
- Not actively maintained (last commit was in 2018)
Code Examples
- Creating a thread pool and submitting a simple task:
#include "threadpool.h"
ThreadPool pool(4); // Create a thread pool with 4 worker threads
auto result = pool.enqueue([](int a, int b) { return a + b; }, 3, 4);
std::cout << "Result: " << result.get() << std::endl; // Output: Result: 7
- Submitting multiple tasks and retrieving results:
std::vector<std::future<int>> results;
for (int i = 0; i < 8; ++i) {
results.emplace_back(
pool.enqueue([i] {
std::this_thread::sleep_for(std::chrono::seconds(1));
return i * i;
})
);
}
for (auto& result : results) {
std::cout << result.get() << ' ';
}
// Output: 0 1 4 9 16 25 36 49
- Using the thread pool with a custom function:
int fibonacci(int n) {
if (n <= 1) return n;
return fibonacci(n - 1) + fibonacci(n - 2);
}
auto fib_result = pool.enqueue(fibonacci, 20);
std::cout << "Fibonacci(20) = " << fib_result.get() << std::endl;
// Output: Fibonacci(20) = 6765
Getting Started
To use the thread pool in your C++ project:
- Download the
threadpool.h
file from the repository. - Include the header in your C++ file:
#include "threadpool.h"
- Create a ThreadPool object with the desired number of worker threads:
ThreadPool pool(std::thread::hardware_concurrency());
- Submit tasks to the pool using the
enqueue
method:auto result = pool.enqueue([]() { return "Hello, Thread Pool!"; }); std::cout << result.get() << std::endl;
That's it! You can now use the thread pool to parallelize your workloads efficiently.
Competitor Comparisons
Modern and efficient C++ Thread Pool Library
Pros of CTPL
- More feature-rich with support for future/promise-like functionality
- Better exception handling and propagation
- More flexible task queueing with priority support
Cons of CTPL
- Slightly more complex API compared to threadpool
- Potentially higher memory overhead due to additional features
- May have a steeper learning curve for beginners
Code Comparison
CTPL:
ctpl::thread_pool p(4);
auto future = p.push([](int id){ return id; });
std::cout << future.get() << std::endl;
threadpool:
threadpool pool(4);
pool.enqueue([](){ /* task */ });
Key Differences
- CTPL offers more advanced features like futures and priorities
- threadpool has a simpler, more straightforward API
- CTPL provides better control over task execution and results
- threadpool is lighter-weight and easier to integrate for basic use cases
Both libraries aim to provide thread pool functionality for C++, but CTPL offers a more comprehensive set of features at the cost of increased complexity. The choice between the two depends on the specific requirements of the project and the desired level of control over thread management and task execution.
Thread pool implementation using c++11 threads
Pros of thread-pool
- More comprehensive documentation and examples
- Supports both C++11 and C++14 standards
- Includes unit tests for better reliability
Cons of thread-pool
- Larger codebase, potentially more complex to integrate
- Lacks some advanced features like thread affinity
Code Comparison
thread-pool:
void ThreadPool::Init() {
const uint32_t num_threads = std::thread::hardware_concurrency();
for (uint32_t i = 0; i < num_threads; i++) {
m_threads.emplace_back(&ThreadPool::WorkerThread, this);
}
}
threadpool:
void ThreadPool::start(int threads) {
for (int i = 0; i < threads; ++i)
workers.emplace_back([this] {
while (true) {
std::function<void()> task;
{
std::unique_lock<std::mutex> lock(this->queue_mutex);
this->condition.wait(lock, [this] { return this->stop || !this->tasks.empty(); });
if (this->stop && this->tasks.empty()) return;
task = std::move(this->tasks.front());
this->tasks.pop();
}
task();
}
});
}
The thread-pool implementation uses a separate initialization method, while threadpool combines thread creation and task execution logic in a single method. threadpool's approach may be more compact but potentially less flexible for customization.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
threadpool
based on C++11 , a mini threadpool
, accept variable number of parameters.
åºäºC++11ç线ç¨æ± ,ç®æ´ä¸å¯ä»¥å¸¦ä»»æå¤çåæ°
管çä¸ä¸ªä»»å¡éåï¼ä¸ä¸ªçº¿ç¨éåï¼ç¶åæ¯æ¬¡åä¸ä¸ªä»»å¡åé ç»ä¸ä¸ªçº¿ç¨å»åï¼å¾ªç¯å¾å¤ã æææçæ¯,éå¶åªå建ä¸ä¸ªçº¿ç¨,è¿æ ·å°±æ¯ä¸ä¸ªå®å ¨çä»»å¡éåäºã
线ç¨æ± ,å¯ä»¥æ交ååå½æ°ææå§è¾¾è¡¨è¾¾å¼çå¿åå½æ°æ§è¡,å¯ä»¥è·åæ§è¡è¿åå¼
代ç ä¸å¤,**ä¸ç¾è¡ä»£ç å°±å®æäº çº¿ç¨æ± **, 并ä¸, çç commit
, å, ä¸æ¯åºå®åæ°ç, æ åæ°æ°ééå¶! è¿å¾çäºå¯ååæ°æ¨¡æ¿.
æ¯æèªå¨éæ¾å¤ä½ç©ºé²çº¿ç¨,é¿å å³°å¼è¿åå¾å¤å¤ä½ç空é²è¿ç¨, 线ç¨æ´ä¼é çç»æ.
*为äºé¿å«ï¼å è¿è¡ä¸ä¸çæ说æï¼ä»£ç æ¯ me âåâçï¼ä½æ¯æè·¯æ¥èª Internetï¼ ç¹å«æ¯è¿ä¸ªçº¿ç¨æ± å®ç°(åºæ¬ copy äºè¿ä¸ªå®ç°,å ä¸è¿ä½åå¦çå®ç°å解éï¼å¥½ä¸è¥¿å¼å¾ copy ! * ç¶å综åæ´æ¹äºä¸,æ´å ç®æ´ )ã
##C++11è¯è¨ç»è å³ä½¿æåçä¹ä¸ä»£è¡¨è½ååºç¨åºï¼ä¸é¢ç¨äºä¼å¤c++11çâå¥ææ·«å·§âï¼ä¸é¢ç®åæè¿°ä¹ã
- using Task = function<void()> æ¯ç±»åå«åï¼ç®åäº typedef çç¨æ³ãfunction<void()> å¯ä»¥è®¤ä¸ºæ¯ä¸ä¸ªå½æ°ç±»åï¼æ¥åä»»æååæ¯ void() çå½æ°ï¼ææ¯å½æ°å¯¹è±¡ï¼ææ¯å¿åå½æ°ãvoid() æææ¯ä¸å¸¦åæ°ï¼æ²¡æè¿åå¼ã
- pool.emplace_back([this]{...}) å pool.push_back([this]{...}) åè½ä¸æ ·ï¼åªä¸è¿åè æ§è½ä¼æ´å¥½ï¼
- pool.emplace_back([this]{...}) æ¯æé äºä¸ä¸ªçº¿ç¨å¯¹è±¡ï¼æ§è¡å½æ°æ¯æå§è¾¾å¿åå½æ° ï¼
- ææ对象çåå§åæ¹å¼åéç¨äº {}ï¼èä¸åä½¿ç¨ () æ¹å¼ï¼å 为é£æ ¼ä¸å¤ä¸è´ä¸å®¹æåºéï¼
- å¿åå½æ°ï¼ [this]{...} ä¸å¤è¯´ã[] æ¯ææå¨ï¼this æ¯å¼ç¨åå¤çåé thisæéï¼ å é¨ä½¿ç¨æ»å¾ªç¯, ç±cv_task.wait(lock,[this]{...}) æ¥é»å¡çº¿ç¨ï¼
- delctype(expr) ç¨æ¥æ¨æ expr çç±»åï¼å auto æ¯ç±»ä¼¼çï¼ç¸å½äºç±»åå ä½ç¬¦ï¼å æ®ä¸ä¸ªç±»åçä½ç½®ï¼auto f(A a, B b) -> decltype(a+b) æ¯ä¸ç§ç¨æ³ï¼ä¸è½åä½ decltype(a+b) f(A a, B b)ï¼ä¸ºå¥ï¼ï¼ c++ å°±æ¯è¿ä¹è§å®çï¼
- commit æ¹æ³æ¯ä¸æ¯ç¥å¥è©ï¼å¯ä»¥å¸¦ä»»æå¤çåæ°ï¼ç¬¬ä¸ä¸ªåæ°æ¯ fï¼åé¢ä¾æ¬¡æ¯å½æ° f çåæ°(注æ:åæ°è¦ä¼ struct/classçè¯,建议ç¨pointer,å°å¿åéçä½ç¨å)ï¼ å¯ååæ°æ¨¡æ¿æ¯ c++11 çä¸å¤§äº®ç¹ï¼å¤äº®ï¼è³äºä¸ºä»ä¹æ¯ Arg... å arg... ï¼å 为è§å®å°±æ¯è¿ä¹ç¨çï¼
- commit ç´æ¥ä½¿ç¨æºè½è°ç¨stdcallå½æ°ï¼ä½æ两ç§æ¹æ³å¯ä»¥å®ç°è°ç¨ç±»æåï¼ä¸ç§æ¯ä½¿ç¨ bindï¼ .commit(std::bind(&Dog::sayHello, &dog))ï¼ ä¸ç§æ¯ç¨ mem_fnï¼ .commit(std::mem_fn(&Dog::sayHello), &dog)ï¼
- make_shared ç¨æ¥æé shared_ptr æºè½æéãç¨æ³å¤§ä½æ¯ shared_ptr
p = make_shared (4) ç¶å *p == 4 ãæºè½æéç好å¤å°±æ¯ï¼ èªå¨ delete ï¼ - bind å½æ°ï¼æ¥åå½æ° f åé¨ååæ°ï¼è¿åcurryingåçå¿åå½æ°ï¼è¬å¦ bind(add, 4) å¯ä»¥å®ç°ç±»ä¼¼ add4 çå½æ°ï¼
- forward() å½æ°ï¼ç±»ä¼¼äº move() å½æ°ï¼åè æ¯å°åæ°å³å¼åï¼åè æ¯... è¿ä¹è¯´å¢ï¼å¤§æ¦ææå°±æ¯ï¼ä¸æ¹åæåä¼ å ¥çç±»åçå¼ç¨ç±»å(å·¦å¼è¿æ¯å·¦å¼ï¼å³å¼è¿æ¯å³å¼)ï¼
- packaged_task å°±æ¯ä»»å¡å½æ°çå°è£ ç±»ï¼éè¿ get_future è·å future ï¼ ç¶åéè¿ future å¯ä»¥è·åå½æ°çè¿åå¼(future.get())ï¼packaged_task æ¬èº«å¯ä»¥åå½æ°ä¸æ ·è°ç¨ () ï¼
- queue æ¯éåç±»ï¼ front() è·å头é¨å ç´ ï¼ pop() 移é¤å¤´é¨å ç´ ï¼back() è·åå°¾é¨å ç´ ï¼push() å°¾é¨æ·»å å ç´ ï¼
- lock_guard æ¯ mutex ç stack å°è£ ç±»ï¼æé çæ¶å lock()ï¼ææçæ¶å unlock()ï¼æ¯ c++ RAII ç ideaï¼
- condition_variable cv; æ¡ä»¶åéï¼ éè¦é å unique_lock 使ç¨ï¼unique_lock ç¸æ¯ lock_guard ç好å¤æ¯ï¼å¯ä»¥éæ¶ unlock() å lock()ã cv.wait() ä¹åéè¦ææ mutexï¼wait æ¬èº«ä¼ unlock() mutexï¼å¦ææ¡ä»¶æ»¡è¶³åä¼éæ°ææ mutexã
- æå线ç¨æ± ææçæ¶å,join() å¯ä»¥çå¾ ä»»å¡é½æ§è¡å®å¨ç»æ,å¾å®å ¨!
Top Related Projects
Modern and efficient C++ Thread Pool Library
Thread pool implementation using c++11 threads
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot