Tuesday, March 28, 2017

Asio ThreadPool Performance Test

Updated: 7 April 2017

Category: C++, Concurrency Programming, Thread pool, Asio

I have tested Asio ThreadPool performance by comparing processing time of three concurrency methods. The first method is Standard C++ Multithreading (mthread), the second is Asio ThreadPool (asio) and the last is Microsoft Parallel Patterns Library (ppl). All of them were used for calculating PI and Fibonacci numbers on the same machine for both Windows and Linux. They all used the same code except for PPL that is only available on Windows. I have also measured serial processing times for baseline comparison. The C++ compilers used are MSVC 19.10.25017 for Windows, GCC 6.3.1 and Clang 3.9.1 for Linux. The results are as follow:



Comparing time used between MSVC, GCC and Clang




GCC is the fastest in all scenarios (not including ppl). The second is MSVC except for fibonacci with asio that Clang comes the second.
 

Comparing each concurrency method with serial processing


Using MSVC
There is no big difference in time used for PI among mthread, asio and ppl. However for Fabonacci, mthread and ppl approximately process at the same speed and faster than asio which is the slowest.





Using GCC
There is no big difference between mthread and asio in time used for PI. But for Fabonacci, mthread has a little bit faster than asio.




Using Clang
There is no big difference between mthread and asio for both PI and Fibonacci.


Conclusions
  • The concurrency processing speed depends mainly on three factors: compiler, computational type and concurrency method respectively. 
  • Asio ThreadPool does not perform well with MSVC on some computational type (Fibonacci) when comparing with GCC and Clang.
The code is here.


Saturday, March 4, 2017

Using Asio C++ library based ThreadPool class

Category: C++, Concurrency Programming, Thread pool, Asio

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:
  1. To use C++ multithreading with thread pool.
  2. To use task based concurrency programming.
  3. Can use strictly sequential invocation of handlers.
  4. Can use C++ Exception handling.

How can you use it?
  1. Create the client class that will use thread pool.
  2. Create any tasks that you want to execute in sequential or parallel order. In our case we use sequential for simplicity.
  3. If you want to handle exception. Modify previous code, add final task to start and stop MainIoService as shown in the following code.
  4. 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.
  5. If you use _threadPool.strand() instead of _threadPool.enqueue(), any tasks called can not be executed concurrently.
How to dowload the ThreadPool Class?
The ThreadPool Class is in ThreadPool.h header file on github.