This is my second article on multi-threaded programming. I highly recommend you to read Multi-threaded Programming 1 before proceeding any further.
Concurrent programming, Parallel programming and Asynchronous programming are often spoken about in the same breath. However they aren’t necessarily similar.
In concurrent programming, tasks that are seemingly executed in parallel are not in-fact executed at the same time. Here, the executioner (eg:-OS) juggles between multiple tasks. When a portion of a certain task is completed, it’s state is saved and is put to sleep. Executioner then picks up the next task and follows the same procedure. This continues until all the tasks are completed.
In parallel programming, tasks that are executed in parallel are truly executed in parallel via multiple threads of execution. Each logical CPU executes a unique task and all of them run at the same time.
In asynchronous programming, there is often a background task that needs to executed by the main program but the main program does not want to wait until this task finishes execution. The background task could be external, ie, it may be carried out by an external application. In this way, the main program is not blocked and therefore can continue execution while the external application runs the background task.
There are a few different ways to write multi-threaded code in C++. Let us start with the low level constructs and slowly proceed towards the higher level constructs.
std::thread
Creating a thread manually is the traditional way to write multi-threaded code. We can then pass to this thread, a reference to a function. It is important to either join() or detach() this newly created thread from the parent thread. Failure to do so will result in a run-time error when the child thread returns. If some value needs to be returned from the child thread to the parent thread, we can use constructs like std::promise and std::future. Both std:: promise and std::future are objects that give us access to a shared state between threads. While std:: promise lets us write to the shared state, std::future lets us read from the shared state. So what we need to do is to create an std::promise object in the parent thread and pass it to the newly created child thread. We can get the std::future object referring to the same shared state as the std::promise object by calling the get_future() method of the std::promise object. Please note std::promise is not copyable. So it cannot be passed by value to the child thread. It has to be either passed by reference or moved (ie transfer ownership via std::move).
Following example illustrates the use of these constructs.
#include <iostream> | |
#include <thread> | |
#include <future> | |
using namespace std; | |
void func(promise<int>&& a) { | |
a.set_value(4); | |
} | |
int main() { | |
promise<int> pr; | |
auto fut = pr.get_future(); | |
thread t(&func,move(pr)); | |
t.detach(); | |
cout << fut.get(); | |
cin.get(); | |
return 0; | |
} |
Programs typically use what is known as ‘thread-pools’ to perform tasks asynchronously. A thread-pool can be thought of as an object that holds a queue of jobs and a certain number of worker threads that pops jobs off this queue and execute them. Whenever a task needs to be completed, the main thread (or any other thread that uses this thread-pool) wraps this task into a job and adds it to the thread-pool’s queue along with an optional callback. A worker thread that executes this job invokes this callback to let the main thread know once it completes the job.
The C++ standard library does not provide a built-in thread-pool unfortunately. Therefore, it is up to us to implement our own thread-pool, if need be. But I am pretty sure there are libraries out there that provide well equipped thread-pools that could even be dynamic in nature. Dynamic thread-pools generally have a built-in scaling mechanism to create additional threads based on demand.
Following example shows a simple implementation of a thread-pool class.
#include <iostream> | |
#include <functional> | |
#include <mutex> | |
#include <thread> | |
#include <vector> | |
#include <queue> | |
#include <atomic> | |
class Threadpool | |
{ | |
std::queue<std::function<void()>> d_queue; | |
std::vector<std::shared_ptr<std::thread>> d_threads; | |
int d_numThreads; | |
int d_queueSize; | |
std::mutex d_mutex; | |
std::atomic<bool> d_stopFlag; | |
public: | |
Threadpool(int threads, int s); | |
bool start(); | |
bool stop(); | |
int enqueue(std::function<void()> func); | |
private: | |
void execute(); | |
}; | |
Threadpool::Threadpool(int threads, int s) : d_numThreads(threads), d_queueSize(s) {} | |
int Threadpool::enqueue(std::function<void()> func) | |
{ | |
std::lock_guard<std::mutex> lg(d_mutex); | |
if (d_queue.size() == d_queueSize) | |
{ | |
return 1; | |
} | |
d_queue.push(func); | |
return 0; | |
} | |
bool Threadpool::start() | |
{ | |
for (int i = 0; i < d_numThreads; ++i) | |
{ | |
std::shared_ptr<std::thread> sp(new std::thread(&Threadpool::execute, this)); | |
d_threads.push_back(sp); | |
} | |
} | |
bool Threadpool::stop() | |
{ | |
d_stopFlag = true; | |
for (auto sp : d_threads) | |
{ | |
sp->join(); | |
} | |
} | |
void Threadpool::execute() | |
{ | |
std::function<void()> job; | |
while (!d_stopFlag) | |
{ | |
{ | |
std::lock_guard<std::mutex> lg(d_mutex); | |
job = d_queue.front(); | |
d_queue.pop(); | |
} | |
job(); | |
} | |
} |
std::packaged_task
std:: packaged_task is a higher level construct than std::thread that makes it easier to write multi-threaded code. It can wrap a callable target and provides an std::future to fetch the returned value of the target. Note that the packaged task can either be invoked from the same thread or from a different thread but in either case, it needs to be invoked explicitly. The following example would hopefully clarify any doubts you may still have.
#include <iostream> | |
#include <future> | |
#include <thread> | |
int someTask(){ | |
std::cout << "Some task\n"; | |
} | |
int main(){ | |
// Execute the task in the same thread. | |
std::packaged_task<void()> task1(someTask); | |
auto f1 = task1.get_future(); | |
task(); | |
int ret1 = f1.get(); | |
// Execute the task in a separate thread. | |
std::packaged_task<void()> task2(someTask); | |
auto f2 = task2.get_future(); | |
std::thread(task); | |
int ret2 = f2.get(); | |
} |
You can read more about std::packaged_task here.
std::async
std::async is an even higher level construct than std::packaged_task that further simplifies the creation of multi-threaded code. Similar to an std::packaged_task, std::async allows a callable target to be specified to it and provides an std::future to fetch the returned value. Unless std::launch::async is specified in its constructor, the callable target is most likely invoked from within the same thread.
#include <iostream> | |
#include <future> | |
#include <thread> | |
int someTask(){ | |
std::cout << "Some task\n"; | |
} | |
int main(){ | |
// The task may or may not get executed in a separate thread. | |
auto f1 = std::async(someTask); | |
int ret1 = f1.get(); | |
// The task is guaranteed to execute in a separate thread. | |
auto f2 = std::async(std::launch::async, someTask); | |
int ret2 = f2.get(); | |
} |
You can read more about std::async here.