Parallel Execution for Contests Part 3 — C++

This is the third part of the Parallel execution for contests series. For your convenience you can find other parts in the table of contents in Part 1 — Powershell

Hello! Today we continue working on parallel codes for contests. Let’s go.

Idea

Last time we saw how to implement simple thread pooling in C# using ManualResetEvents. We basically want to achieve the same using C++ mechanisms. We are going to utilize constructs from C++11 in order to write portable code. I performed the tests using VC++ 2015.

Implementation

We first start with simple ThreadPool available on GitHub
We start with the following implementation:

Next, we implement custom ManualResetEvent:

Finally, we implement custom function to queue tasks:

The code is very similar to C# implementation. Finally, we want to execute the code:

Summary

This simple thread pool allows us to run multiple test cases in parallel. We also don’t need to modify our algorithm really much — all we need to do is invoke two signal functions in correct order and everything else will go smoothly. Using this C++ code and Powershell code from the part 1 we can easily execute multiple tests in parallel and save some time when our algorithm is not optimal.