diff --git a/README.md b/README.md index 9f0c2b6..94a529e 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ CTPL ==== -Modern and efficient C++ Thread Pool Library +## Modern and efficient C++ Thread Pool Library A thread pool is a programming pattern for parallel execution of jobs, http://en.wikipedia.org/wiki/Thread_pool_pattern. @@ -10,7 +10,8 @@ More specifically, there are some threads dedicated to the pool and a container A thread pool is helpful when you want to minimize time of loading and destroying threads and when you want to limit the number of parallel jobs that run simultanuasly. For example, time consuming event handlers may be processed in a thread pool to make UI more responsive. -Features: +## Features + - standard c++ language, tested to compile on MS Visual Studio 2013 (2012?), gcc 4.8.2 and mingw 4.8.1(with posix threads) - simple but effiecient solution, one header only, no need to compile a binary library - query the number of idle threads and resize the pool dynamically @@ -23,37 +24,41 @@ Features: - two variants, one depends on Boost Lockfree Queue library, http://boost.org, which is a header only library -Sample usage +## Sample usage -void first(int id) { +```c++ +void first(int id) { std::cout << "hello from " << id << '\n'; -} +} - struct Second { +struct Second { void operator()(int id) const { std::cout << "hello from " << id << '\n'; } } second; -void third(int id, const std::string & additional_param) {} - - -int main () { - - ctpl::thread_pool p(2 /* two threads in the pool */); +void third(int id, const std::string & additional_param) {} - p.push(first); // function +int main () { + // Create a threadpool with 2 threads + ctpl::thread_pool p(2); - p.push(third, "additional_param"); - - p.push( [] (int id){ - std::cout << "hello from " << id << '\n'; -}); // lambda - - p.push(std::ref(second)); // functor, reference + // Add regular functions to the threadpool + p.push(first); + p.push(third, "additional_param"); + + // Add lambdas to the threadpool + p.push( [] (int id){ + std::cout << "hello from " << id << '\n'; + }); // lambda - p.push(const_cast<const Second &>(second)); // functor, copy ctor + // functor, reference + p.push(std::ref(second)); - p.push(std::move(second)); // functor, move ctor + // functor, copy constructor + p.push(const_cast(second)); -} + // functor, move ctor + p.push(std::move(second)); +} +```