Helpers For Multithreading in C++

On of the most important challenges nowadays in programming is concurrency. If we don’t learn to write programs that are able to run on multiple cores the progress in hardware will be pointless. But when you run multiple threads for various processing you might face the situation when you have to write over and over again same or similar code for creating the threads, setting up the parameters for the threads, joining the threads, checking the result, cleaning-up, etc.

In this post I will show how you can create some helpers in C++ to simplify this process. This is not going to be a full solution, neither a solution that fits all needs, but can be a start.

What I would like to have is a helper class that will take care of:

  • finding how many threads can run (considering each available core can run a thread)
  • creating and starting the threads
  • joining the threads
  • checking the result of the threads execution
  • cleaning up

The class show bellow does just that.

This helper class contains:

  • one parameter-less constructor that identifies the number of available processors and sets the threads count equal to the processors count
  • one constructor that takes the number of threads that should be created
  • one method (SetThreadParams) for setting the parameters for each thread that will be created
  • one method (Run) that creates and runs the thread, waits for them and checks the result of the execution

As you can see the Run() method is simplistic. It does not handle for instance timed out or abandoned thread executions. Also it joins all threads, waiting until all of them finished execution. A more flexible method could wait only until the first thread finishes and then maybe closes the other threads. But as I said, this is a sample and not a complete solution.

Having this helper set up, I will start several threads to find the prime numbers in a sequence and print them in the console.

The following function computes whether a number is prime/

The thread procedure will run through a sub-sequence of a vector of integers and verify if each element is prime. I will use the following structure to pass the sequence bounds to the thread procedure:

The thread procedure could look like this:

To print to the console a locking mechanism is necessary, otherwise prints from two different threads could collide. The critical section will be initialized before the threads are started.

What remains to be done is generating a sequence of integers, setting up the parameters with the sequence bounds for each thread and run the threads using the helper.

Having this threads helper class, what I need to do when running some processing in several threads is:

  • setup thread parameters (if the case)
  • write the thread procedure
  • create a ThreadHelper object and initialize it
  • run the threads and collect the results

The helper class prevents writing same code over and over again and helps focusing on the most important tasks: writing the thread procedure. As I said earlier it is not a full solution, nor one that fits all scenarios, but you can develop it to suit your needs.

1 Reply to “Helpers For Multithreading in C++”

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.