Learn C++ Multi-Threading in 5 Minutes

Summary: A crash course on the C++14 multi-threading constructs in a very non-verbose manner

Ari Saif
Ari Saif
Oct 28, 2018 · 5 min read
Photo credit: Wikipedia

The new C++ multi-threading constructs are very easy to learn. If you are familiar with C or C++ and want to start writing multithreaded programs, this article is for you!

I use C++14 as a reference, but what I describe is also supported in C++17 . I only cover common constructs. You should be able to write your own multithreaded programs after reading this.

Creating Threads

A thread can be created in several ways:

  1. Using a function pointer
  2. Using a functor
  3. Using a lambda function

These methods are very similar with minor differences. I explain each method and their differences next.

Using a function pointer

Consider the following function which takes a vector reference v, a reference to the result acm, and two indices in the vector v. The function adds all elements between beginIndex and endIndex.

A function calculating the sum of all elements between beginIndex and endIndex in a vector v

Now lets say you want to partition the vector in two sections and calculate the total sum of each section in a separate thread t1 and t2 :

Creating threads using function pointers

What do you need to take away?

  1. std::thread creates a new thread. The first parameter is the name of the function pointer accumulator_function2. Therefore, each thread will execute this function.
  2. The rest of the parameters passed to std::thread constructor are the parameters that we need to pass to accumulator_function2.
  3. Important: All parameters passed to accumulator_function2 are passed by value unless you wrap them in std::ref. That’s why we wrapped v , acm1 , and acm2 in std::ref.
  4. Threads created by std::thread do not have return values. If you want to return something, you should store it in one of the parameters passed by reference, i.e. acm .
  5. Each thread starts as soon as it gets created.
  6. We use join() function to wait for a thread to finish

Using Functors

You can do exactly the same thing using functors. The following is the code that uses a functor:

Functor Definition

And the code that creates the threads is:

Creating threads using functors

What do you need to take away?

Everything is very similar to function pointer, except that:

  1. The first parameter is the functor object.
  2. Instead of passing a reference to the functor to store the result, we can store its return value in a member variable inside the functor, i.e. in _acm.

Using Lambda Functions

As the third alternative we can define each thread in a lambda function as shown below:

Creating threads using lambda functions

Again, everything is very similar to function pointer, except that:

  1. As an alternative to pass a parameter, we can pass references to lambda functions using lambda capture.

Tasks, Futures, and Promises

As an alternative to std::thread, you can use tasks.

Tasks work very similar to threads, but the main difference is that they can return a value. So, you can remember them as a more abstract way of defining your threads and use them when the threads return a value.

Below is the same example written using tasks:

What do you need to take away?

  1. Tasks are defined and created using std::async , (instead of threads that are created using std::thread)
  2. The returned value from std::async is called a std::future. Don’t get scared by its name. It just means t1 and t2 are variables whose value will be assigned to in the future. We get their values by calling t1.get() and t2.get()
  3. If the future values are not ready, upon calling get() the main thread blocks until the future value becomes ready (similar to join() ).
  4. Notice that the function that we passed to std::async returns a value. This value is passed through a type called std::promise. Again, don’t get scared by its name. For the most part, you don’t need to know details of std::promiseor define any variable of type std::promise. The C++ library does that behind the scenes.
  5. Each task by default starts as soon as it is created (there is a way to change this which I don’t cover).

Summary of Creating Threads

There you have it. Creating threads is as simple as what I explained above. You can either use std::thread:

  1. Use function pointers
  2. Use functors
  3. Use lambda functions

Or you can use std::async to create a task and get the return values in a std::future. Tasks can get also use a function pointer, a functor, or a lambda function.

Shared Memory and Shared Resources

In short, threads should be careful when they read/write into shared memory and resources (such as files) to avoid race conditions.

C++14 provides several constructs to synchronize threads to avoid such race conditions.

Using Mutex, lock,() and unlock() (Not recommended)

The following code shows how we create a critical section such that each thread accesses std::cout exclusively:

What do you need to take away?

  1. A mutex is created std::mutex
  2. A critical section (i.e. guaranteed to be run only by a single thread at each time) is created using lock()
  3. The critical section ends upon calling unlock()
  4. Each thread waits at lock() and only enters the critical section if no other thread is inside that section.

While the above method works, it is not recommended because:

  1. It is not exception safe: if the code before lock generates an exception, unlock()will not be executed, and we never release the mutex which might cause deadlock
  2. We always have to be careful not to forget to call unlock()

Using std::lock_guard (recommended)

Don’t get scared by its name lock_guard. It’s just a more abstract way of creating critical sections.

Below is the same critical section using lock_guard.

critical section using lock_guard

What do you need to take away?

  1. The code coming after std::lock_guard creation is automatically locked. No need for explicit lock() and unlock() function calls.
  2. The critical section automatically ends when std::lock_guard goes out of scope. This makes it exception safe, and also we don’t need to remember to call unlock()
  3. lock_guard still requires using a variable of type std::mutex in its constructor.

How Many Threads Should We Create?

You can create as many threads as you want, but it would probably be pointless if the number of active threads is more than the number of available CPU cores.

In order to get the maximum number of cores you can call: std::thread::hardware_concurrency() as shown below:

What I Didn’t Cover

I covered most of what you need to create threads. There are several other details that are less common which I don’t include here, but you can study them on your own:

  1. std::move
  2. details of std::promise
  3. std::packaged_task
  4. Conditional variables

Hope this helps you learning C++ multi threading quickly.

If you liked this article please click on the clap and give me feedback.

HackerNoon.com

how hackers start their afternoons.

Ari Saif

Written by

Ari Saif

Ari is the co-founder and the main developer of the following mobile apps: BitcoinCrazYness: www.bitcoinCrazyness.com, and PlusOne Social www.okPlusOne.com

HackerNoon.com

how hackers start their afternoons.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade