thread pool written in c++ 11 with std::thread + std::future
$ mkdir build && cd build
$ cmake .. && make
$ sudo make install
[ 50%] Built target inkneko_thread_pool
[100%] Built target thread_pool_test
Install the project...
-- Install configuration: ""
-- Installing: /usr/local/include/nekorpc/ThreadPool.h
-- Installing: /usr/local/lib/libinkneko_thread_pool.so$ tests/thread_pool_test 1 1000 1
1000 times plus finished, with latency set: 1ms, threadNum: 1
counter value: 1000, time elapsed: 1.26811
1000 times plus finished, with latency set: 1ms, threadNum: 1
counter value: 1000, time elapsed: 1.27039
1000 times plus finished, with latency set: 1ms, threadNum: 1
counter value: 1000, time elapsed: 1.26788
$ tests/thread_pool_test 10 1000 1
1000 times plus finished, with latency set: 1ms, threadNum: 10
counter value: 1000, time elapsed: 0.12013
1000 times plus finished, with latency set: 1ms, threadNum: 10
counter value: 1000, time elapsed: 0.123693
1000 times plus finished, with latency set: 1ms, threadNum: 10
counter value: 1000, time elapsed: 0.123175
$ tests/thread_pool_test 100 1000 1
1000 times plus finished, with latency set: 1ms, threadNum: 100
counter value: 1000, time elapsed: 0.0298044
1000 times plus finished, with latency set: 1ms, threadNum: 100
counter value: 1000, time elapsed: 0.035674
1000 times plus finished, with latency set: 1ms, threadNum: 100
counter value: 1000, time elapsed: 0.0312232To be continued... You can check out the test.
Mainly in these procedures:
- Create thread pool with:
ThreadPool threadPool(threadNum); - Find a place to save your task, like
std::vector<std::future<TaskFuncRetType>> tasks; - Call
tasks.emplace_back(threadPool.addTask(taskfunc))to leave your task function executed by the thread. - Use
std::future::wait_for(timeout)to check weather the task finished. - Use
std::future::get()to obtain the task function return value.