Prerequisites: C++, Asio C++ programming concept, Multithreading
Requirement: Asio C++ library
What is it?
It is only a C++ header file that defines ThreadPool class based on Asio C++ Library.
Why is it created?
It is created to serve the following purposes:
- To use C++ multithreading with thread pool.
- To use task based concurrency programming.
- Can use strictly sequential invocation of handlers.
- Can use C++ Exception handling.
How can you use it?
- Create the client class that will use thread pool.
- Create any tasks that you want to execute in sequential or parallel order. In our case we use sequential for simplicity.
- If you want to handle exception. Modify previous code, add final task to start and stop MainIoService as shown in the following code.
- In the main code, create ThreadPool instance. Then create client instance by using shared_ptr. Make a call from the client and try to throw an exception in the client code to test exception handling.
- If you use _threadPool.strand() instead of _threadPool.enqueue(), any tasks called can not be executed concurrently.
The ThreadPool Class is in ThreadPool.h header file on github.
No comments:
Post a Comment