ALLCODE CRAFT

C++11 Multithreading Tutorial via Q&A – Thread Management Basics

In the highly acclaimed book "The Seven Habits Of Highly Effective People", Steven Covey states that we can greatly amplify our chances of success when we "Begin with the End in Mind".

For me, this means starting with a set of questions whenever I'm trying to learn or teach something new. I've had very good results with this format of learning as opposed to the regular tutorial/text-book model.

In the same spirit, here's the first installment of a no frills tutorial on C++11 multithreading and Concurrency using Q&A format. It's a long article, so here's a snapshot of what we'll be covering. Feel free to skip to a topic of interest using the links below.

Questions At A Glance

1. What header file should you include for using C++11 multithreading capabilities ?
2. What are the different ways of creating a thread in C++11 ?
3. How can a C++11 thread be created with a function pointer ?
4. How can a C++11 thread be created with a function Object ?
5. How can a C++11 thread be created with a lambda ?
6. Can a lambda closure be used to create a C++11 thread ?
7. How can a C++11 thread be created with a member function ?
8. What does joining C++11 threads mean ? Alternatively what does the std::thread::join() do ?
9. Can you name a situation where joining threads should be avoided ?
10. Can you create a C++11 thread with a function pointer that takes a bunch of arguments ?
11. Can you create a C++11 thread with a lambda closure that takes a bunch of arguments ?
12. Are the arguments passed to a C++11 thread's constructor pass by value or pass by reference ?
13. How can we pass C++11 thread arguments by reference ?
14. Does a C++11 thread act on a specific instance of a function object ?
15. How can you create background tasks with C++11 threads ?
16. Can the ownership of C++11 threads be transferred at runtime ?
17. What will happen if in the above example we tried to "copy" the thread instead of "move" it ?
18. What is C++11 thread local storage (thread_local) ?
19. How can you retrieve results from a thread ?
20. What is "Oversubscription" ?
21. How can i avoid "Oversubscription" in C++11 when working with multiple threads ?
22. How can you identify different C++11 threads ?

1. What header file should you include for using C++11 multithreading capabilities?

Use the <thread> header file

#include <thread>

Note: The thread functionality is defined in the "std" namespace.

2. What are the different ways of creating a thread in C++11?

There are essentially four ways of creating a thread:

  1. Create a thread with a function pointer
  2. Create a thread with a function object
  3. Create a thread with a lambda
  4. Create a thread with a member function

3. How can a C++11 thread be created with a function pointer?

Just pass in the address of a function to the thread constructor. The thread will start executing the function immediately.

#include "stdafx.h"
#include <thread>
#include <iostream>

using namespace std;

void FireMissile()
{
  cout << "Firing sidewinder missile " << endl;
}

int main()
{
  //Creating a thread with a function pointer
  thread t1(FireMissile);
  t1.join();
  
  return 0;
}

4.How can a C++11 thread be created with a function Object?

Create a function object "Missile" and pass it to the thread constructor.

#include "stdafx.h"
#include <thread>
#include <iostream>

using namespace std;

//Create the function object
class Missile
{
public:
  void operator() () const
  {
    cout << "Firing Tomahawk missile" << endl;
  }
};

int main()
{
  //Creating a thread with an function object
  Missile tomahawk;
  thread t1(tomahawk);
  t1.join();

  return 0;
}

5. How can a C++11 thread be created with a lambda?

#include "stdafx.h"
#include <thread>
#include <iostream>

using namespace std;

int main()
{
  thread t1([] {
    cout << "Launching Scud missile" << endl;
  });
  t1.join();

  return 0;
}

6.Can a lambda closure be used to create a C++11 thread?

Yes ! A lambda closure is nothing but a variable storing a lambda expression. You can store a lambda in a closure if you intend to reuse the lambda expression at more than one place in your code.

#include "stdafx.h"
#include <thread>
#include <iostream>

using namespace std;

int main()
{
  // Define a lambda closure
  auto LaunchMissileFunc = []() -> void { cout << "Launching Cruiser Missile" << endl; };

  thread t1(LaunchMissileFunc);
  t1.join();

  return 0;
}

7.How can a C++11 thread be created with a member function?

#include "stdafx.h"
#include <thread>
#include <iostream>

using namespace std;

class Torpedo
{
public:
  void LaunchTorpedo()
  {
    cout << " Launching Torpedo" << endl;
  }
};

int main()
{
  //Execute the LaunchTorpedo() method for a specific Torpedo object on a seperate thread
  Torpedo torpedo;
  thread t1(&Torpedo::LaunchTorpedo, &torpedo);
  t1.join();

  return 0;
}

Note that here you're executing the LaunchTorpedo() method for a specific Torpedo object on a seperate thread. If other threads are accessing the same "torpedo" object, you'll need to protect the shared resources of that object with a mutex.

8.What does joining C++11 threads mean? Alternatively what does the std::thread::join() do?

A call to std::thread::join() blocks untill the thread on which join is called, has finished executing. In each of the examples above, the join() call ensures that the main method waits for the execution of the spawned threads to finish before it can exit the application.

On the other hand, if we do not call join() after creating a thread in the above case, the main function will not wait for the spawned thread to complete before it tears down the application. If the application tears down before the spawned thread finishes, it will terminate the spawned thread as well, even if it has not finished executing. This can leave data in a very inconsistent state and should be avoided at all cost.

9.Can you name a situation where joining threads should be avoided?

A call to join() blocks the caller thread. This is really bad in situations where the caller thread is a main UI thread – because if the UI thread blocks, the application will stop responding to user inputs which will make it seem hanged.

Another place where calling join() is not advisable is inside a main game loop. Calling join() can block update and rendering of the game scene and severly impact the user experience (it'll be like watching a You tube video on a dial up internet connection !).

10.Can you create a C++11 thread with a function pointer that takes a bunch of arguments?

Yes ! You can just pass the function arguments to the thread constructor. The thread constructor is a variadic template, which means it can accept any number of arguments. Here's an example:

#include "stdafx.h"
#include <string>
#include <thread>
#include <iostream>

using namespace std;

void FireTorpedo(int numCities, string torpedoType)
{
  cout << "Firing torpedo " << torpedoType << " at" << numCities << " cities." << endl;
}

int main()
{
  thread t1(FireTorpedo, 3, "HungryShark");
  t1.join();

  return 0;
}

11.Can you create a C++11 thread with a lambda closure that takes a bunch of arguments?

Yes – just like the previous case, you can pass the arguments needed by the lambda closure to the thread constructor.

auto LaunchTorpedoFunc = [](int numCities, string torpedoType) -> void { cout << "Firing torpedo " << torpedoType << " at" << numCities << " cities." << endl; };

thread t1(LaunchTorpedoFunc, 7, "Barracuda");
t1.join();

12.Are the arguments passed to a C++11 thread's constructor pass by vale or pass by reference?

Thread function arguments are always pass by value, i.e., they are always copied into the internal storage for threads. Any changes made by the thread to the arguments passed does not affect the original arguments. For example, we want the "targetCity" to be modified by the thread but it never happens:

#include "stdafx.h"
#include <string>
#include <thread>
#include <iostream>
#include <functional>

using namespace std;

void ChangeCurrentMissileTarget(string& targetCity)
{
  targetCity = "Metropolis";
  cout << " Changing The Target City To " << targetCity << endl;
}


int main()
{
  string targetCity = "Star City";
  thread t1(ChangeCurrentMissileTarget, targetCity);
  t1.join();
  cout << "Current Target City is " << targetCity << endl;

  return 0;
}

OUTPUT:

Changing The Target City To Metropolis
Current Target City is Star City

Note that the "targetCity" variable is not modified.

13. How can we pass C++11 thread arguments by reference?

We need to use std::ref() from the <functional> header. Consider the following code snippet and associated output.

#include "stdafx.h"
#include <string>
#include <thread>
#include <iostream>
#include <functional>

using namespace std;

void ChangeCurrentMissileTarget(string& targetCity)
{
  targetCity = "Metropolis";
  cout << " Changing The Target City To " << targetCity << endl;
}

int main()
{
  string targetCity = "Star City";
  thread t1(ChangeCurrentMissileTarget, std::ref(targetCity));
  t1.join();
  cout << "Current Target City is " << targetCity << endl;

  return 0;
}

OUTPUT:

Changing The Target City To Metropolis
Current Target City is Metropolis

Notice that the changes to "targetCity" made by the thread was preserved once the thread exited.

14. Does a C++11 thread act on a specific instance of a function object?

No – function objects are copied to the internal storage for the thread. If you need to execute the operation on a specific instance of the function object, you should use std::ref() from <functional> header to pass your function object by reference.

15. How can you create background tasks with C++11 threads?

You can make a std::thread run in the background by calling std::thread::detach() on it. Once detached, a thread continues to run in the background and cannot be communicated with or waited upon to complete. When you detach a thread, the ownership and control passes over to the C++ Runtime Library, which ensures that the resources allocated to the thread are deallocated once the thread exits.

Here's a contrived example. We have a Count() function that prints numbers 1 to 1000 on the screen. If we create a thread to run the function and detach the thread immediately, we'll not see any output – because the main thread terminates before the "Count" thread has had an opportunity to run. To see some of the output, we can put the main thread to sleep for 10 miliseconds which gives the "count" thread to send some of the output to the screen.

#include "stdafx.h"
#include <string>
#include <thread>
#include <iostream>
#include <functional>

using namespace std;

void Count()
{
  for (int i = 0; i < 100; i++)
  {
    cout << "counter at: " << i << endl;
  }
}

int main()
{
  thread t1(Count);
  std::this_thread::sleep_for(std::chrono::milliseconds(10));
  t1.detach();

  return 0;
}

 16. Can the ownership of C++11 threads be transferred at runtime?

Yes. std::thread object owns a resource, where the resource is a current thread of execution. You can call std::move to move the ownership of the underlying resource from one std::thread object to another. The question is – why would you want to do that? Here's a scenario:You want to write a function that creates a thread but does not want to wait for it to finish. Instead it wants to pass the thread to another function which will wait for the thread to finish and execute some action once the execution is done.

#include "stdafx.h"
#include <string>
#include <thread>
#include <iostream>
#include <functional>

using namespace std;


void FireHTTPGet()
{
  std::this_thread::sleep_for(std::chrono::milliseconds(5000));
  cout << "Finished Executing HTTP Get"<< endl;
}

void ProcessHTTPResult(thread t1)
{
  t1.join();
  cout << "HTTP Get Thread Finished Executing - Processing Result Data!" << endl;
}
int main()
{
  thread t11(FireHTTPGet);
  thread t12(ProcessHTTPResult, std::move(t11));

  //Do bunch of other processing without waiting for t11 to finish - instead now we've shouldered off the 
  // responsibility of monitoring t11 thread to t12.

  //Finally wait for t12 to finish
  t12.join();

  return 0;
}

OUTPUT:

Finished Executing HTTP Get
HTTP Get Thread Finished Executing - Processing Result Data!

 17. What will happen if in the above example we tried to "copy" the thread instead of "move" it – like:

thread t11(FireHTTPGet);

thread t12(ProcessHTTPResult, t11)) ?

Your trusted friend , the compiler will give the following error:

error C2664: 'std::tuple<void (__cdecl *)(std::thread),std::thread>::tuple(std::tuple<void (__cdecl *)(std::thread),std::thread> &&)': cannot convert argument 1 from 'void (__cdecl &)(std::thread)' to 'std::allocator_arg_t'

This is compiler's fancy way of telling you that you cannot copy the thread – instead you need to move it.

18. What is C++11 thread local storage (thread_local)?

A thread_local object comes into existence when a thread starts and is destroyed when the thread ends. Each thread has its own instance of a thread-Local object.

To fully understand the implications, let's look at an example- here we'll declare a global variable "globalvar" as thread_local. This'll give each thread it's own copy of globalVar and any modifications made to globalVar will only persist inside that particular thread.In the example below, each of the two threads are modifying globalVar – but they are not seeing each other's change, neither is the main thread.

#include "stdafx.h"
#include <string>
#include <thread>
#include <iostream>
#include <functional>
#include <mutex>

using namespace std;


thread_local int globalVar = 0;
mutex mu;

void PrettyPrint(int valueToPrint)
{
  lock_guard<mutex> lock(mu);
  cout << "Value of globalVar in thread " << this_thread::get_id() << " is " << globalVar << endl;
}

void thread_Local_Test_Func(int newVal)
{
  globalVar = newVal;
  PrettyPrint(globalVar);
}

int main()
{
  globalVar = 1;

  thread t1(thread_Local_Test_Func, 5);
  thread t2(thread_Local_Test_Func, 20);

  t1.join();
  t2.join();

  cout << "Value of globalVar in MAIN thread is " << globalVar << endl;

    return 0;
}

Here's the output of the program – you can see that the three threads (t1, t2 and MAIN) does not see each other's changes to globalVar.

Value of globalVar in thread 17852 is 5
Value of globalVar in thread 29792 is 20
Value of globalVar in MAIN thread is 1

Can you guess what the output will be if globalVar was not declared thread_local ? Here it is :

Value of globalVar in thread 27200 is 5
Value of globalVar in thread 31312 is 20
Value of globalVar in MAIN thread is 20

If the global value was not thread local, the change made by each thread will be persisted outside the thread – here the MAIN thread is feeling the effect  of the change made by t2 and hence printing "20" instead of "1".

19. How can you retrieve results from a thread?

As we'll see in a subsequent tutorial, the easiest and recommended way is to use "futures". However, you can still get the result of some calculation from a thread by either:

  1. Passing reference to a result variable to the thread in which the thread stores the results
  2. Store the result inside a class memeber variable of a function object which can be retrieved once the thread has finished executing.

20. What is "Oversubscription"?

Oversubscription is a situation where more threads are vying for runtime than the underlying hardware can support. One of the biggest cost associated with multiple threads is that of context-switches that happens when the processor switches threads. Ideally, the you'd not want to create more threads than the hardware can support.

21. How can i avoid "Oversubscription" in C++11 when working with multiple threads?

C++11 provides a way to get a hint at the number of threads that can be run in parallel from an application – which most of the time coincides with the number of logical cores.

unsigned int n = std::thread::hardware_concurrency();

On my system with 12 logical cores, it returns 12. This means that I should not attempt to fork more than 12 threads in my application. Note that this is VC++ – other C++ compiler implementations might give different results.

22. How can you identify different C++11 threads?

C++11 gives unique ids to forked threads which can be retrieved using :

  1. By calling the get_id() member function for a specific thread
  2. By calling std::this_thread::get_id() for the currently executing thread

An example of both is given below:

#include "stdafx.h"
#include <string>
#include <thread>
#include <iostream>
#include <functional>

using namespace std;

void Count()
{
  for (int i = 0; i < 100; i++)
  {
    cout << "counter at: " << i << endl;
  }
}

int main()
{
        thread t22(Count);
  
  //Get the ID of the t22 thread
  std::thread::id k = t22.get_id();
  cout << k << endl;
  
  //Get the ID of the MAIN Thread
  std::thread::id j = std::this_thread::get_id();
  cout << j << endl;

  return 0;
}

If I run this code, I can see the thread ids in "threads" and "locals" window. Also note that the thread name is almost useless.

However, the "Location" column can give an indication as to which thread is executing.

Looking Ahead

This article looked at the basics of thread management using the C++ 11 threads library. But it barely scratches the surface of knowledge you need to have to be effective at concurrent programming in C++. To be effective, you'll need to understand the topic of thread synchronization using locks (mutex), exception handling in threads, atomic operations and lockless programming and effective debugging techniques for multithreaded applications. If you've found the article helpful and want to see Q&A on each of the C++ multithreading topics, put a comment or send me an email.

Good luck in your exploration of C++ Threads and Concurrency !