C++11 Thread Pool implementation
use:
- your class extends
Threadand implementsrunandendRunmethods - create threads pool:
ThreadPool<MyClass> threadPool; - get thread if available else wait:
MyClass &myThread = threadPool.getNextThread() - execute
runmethod:myThread.start()
example:
ThreadPool<MyClass> threadPool;
threadPool.setNthread(4);//max 4 running threads
for (int i = 0; i < 100; i++) {
MyClass &myThread = threadPool.getNextThread();
myThread.start();
}
threadPool.joinAll();test directory contains an example, how to find prime numbers from 0 to 100,000 spreading the work on many threads, to compile:
cd test && g++ -pthread -O3 -DDLOG_LEVEL=_INFO -std=c++11 main.cpp MyClass.cpp ../util/String.cpp -o find_prime_number
./find_prime_number N_THREADS