Modern Tutorial
Modern Tutorial
Modern Tutorial
Examples of multithreading
Resources
What is concurrency?
Concurrency occurs when multiple copies of a program run
simultaneously while communicating with each other. Simply put,
concurrency is when two tasks are overlapped. A simple
concurrent application will use a single machine to store the
program’s instruction, but that process is executed by multiple,
different threads. This creates a kind of control flow, where each
thread executes its instruction before passing to the next one.
Methods of Implementing
Concurrency
In C++, the two most common ways of implementing concurrency
are through multithreading and parallelism. While these can be
used in other programming languages, C++ stands out for its
concurrent capabilities with lower than average overhead costs as
well as its capacity for complex instruction. Below, we’ll explore
concurrent programming and multithreading in C++
programming.
C++ Multithreading
C++ multithreading involves creating and using thread objects,
seen as std::thread in code, to carry out delegated sub-tasks
independently. Upon creation, threads are passed a function to
complete, and optionally some parameters for that function.
Image from Medium, [C++] Concurrency by Valentina
Once these tasks are completed, the bees return to the queen for
further instruction. At any one time, there is a set number of these
workers being commanded by the queen, enough to utilize all of its
hive space without overcrowding it.
Parallelism
Creating different threads is typically expensive in terms of both
time and memory overhead for the program; a cost which, when
dealing with short duration, simpler functions, can sometimes not
be worth it. For times like these, developers can instead use
parallel execution policy annotations, a way of marking certain
functions as candidates for concurrency without creating threads
explicitly.
At its most basic, there are two marks that can be encoded into a
function. The first is parallel, which suggests to the compiler that
the function be completed concurrently with other parallel
functions (however the compiler may overrule this suggestion if
resources are limited). The other is sequential, meaning that the
function must be completed individually. Parallel functions can
significantly speed up operations because they automatically use
more of the computer’s CPU resources.
Multithreading Examples
In the following examples, we’ll look at some simple
multithreaded programs designed to use a print function which we
declare at the beginning.
Multi-Thread Example
While the outcome of the single thread example above could easily
be replicated without using multithreaded code, we can truly see
concurrency’s benefits when we attempt to
complete print multiple times with different sets of data. Without
multithreading, this would be done by simply having the main
thread repeat print one at a time until completion.
Let’s see what a many-thread version of the above code would look
like:
C++ concurrency in action: real-
world applications
Multithreading programs are common in modern business
systems, in fact, you likely use some more complex versions of the
above programs in your everyday life.
One example could be an email server, returning mailbox contents
when requested by a user. With this program, we have no way of
knowing how many people will be requesting their mail at any
given time. By using a thread pool, the program can process as
many user requests as possible without risking an overload.