Chapter 18
Concurrency
Modern system architectures usually support running multiple tasks and multiple threads at the same
time. Especially when multiple processor cores are provided, the execution time of programs can
significantly improve when multiple threads are used.
However, executing things in parallel also introduces new challenges. Instead of doing one statement
after the other, multiple statements can be performed simultaneously, which can result in such
problems as concurrently accessing the same resources, so that creations, reads, writes, and deletions
don’t take place in an expected order and provide unexpected results. In fact, concurrent access
to data from multiple threads easily can become a nightmare, with such problems as deadlocks,
whereby threads wait for each other, belonging to the simple cases.
Before C++11, there was no support for concurrency in the language and the C++ standard
library, although implementations were free to give some guarantees. With C++11, this has changed.
Both the core language and the library were improved to support concurrent programming (see
Section 4.5, page 55):
• The core language now defines a memory model that guarantees that updates on two different
objects used by two different threads are independent of each other, and has introduced a new
keyword thread_local for defining variables with thread-specific values.
• The library now provides support to start multiple threads, including passing arguments, return
values, and exceptions across thread boundaries, as well as means to synchronize multiple
threads, so we can synchronize both the control flow and data access.
The library provides its support on different levels. For example, a high-level interface allows you to
start a thread including passing arguments and dealing with results and exceptions, which is based on
a couple of low-level interfaces for each of these aspects. On the other hand, there are also low-level
features, such as mutexes or even atomics dealing with relaxed memory orders.
This chapter introduces these library features. Note that the topic of concurrency and the description
of the libraries provided for it can fill books. So, here, I introduce general concepts and typical
examples for the average application programmer, with the main focus on the high-level interfaces.
For any details, especially of the tricky low-level problems and features, please refer to the specific
books and articles mentioned. My first and major recommendation for this whole topic of concurrency
is the book C++ Concurrency in Action by Anthony Williams (see [Williams:C++Conc]).
Chapter 18
Concurrency
Modern system architectures usually support running multiple tasks and multiple threads at the same
time. Especially when multiple processor cores are provided, the execution time of programs can
significantly improve when multiple threads are used.
However, executing things in parallel also introduces new challenges. Instead of doing one statement
after the other, multiple statements can be performed simultaneously, which can result in such
problems as concurrently accessing the same resources, so that creations, reads, writes, and deletions
don’t take place in an expected order and provide unexpected results. In fact, concurrent access
to data from multiple threads easily can become a nightmare, with such problems as deadlocks,
whereby threads wait for each other, belonging to the simple cases.
Before C++11, there was no support for concurrency in the language and the C++ standard
library, although implementations were free to give some guarantees. With C++11, this has changed.
Both the core language and the library were improved to support concurrent programming (see
Section 4.5, page 55):
• The core language now defines a memory model that guarantees that updates on two different
objects used by two different threads are independent of each other, and has introduced a new
keyword thread_local for defining variables with thread-specific values.
• The library now provides support to start multiple threads, including passing arguments, return
values, and exceptions across thread boundaries, as well as means to synchronize multiple
threads, so we can synchronize both the control flow and data access.
The library provides its support on different levels. For example, a high-level interface allows you to
start a thread including passing arguments and dealing with results and exceptions, which is based on
a couple of low-level interfaces for each of these aspects. On the other hand, there are also low-level
features, such as mutexes or even atomics dealing with relaxed memory orders.
This chapter introduces these library features. Note that the topic of concurrency and the description
of the libraries provided for it can fill books. So, here, I introduce general concepts and typical
examples for the average application programmer, with the main focus on the high-level interfaces.
For any details, especially of the tricky low-level problems and features, please refer to the specific
books and articles mentioned. My first and major recommendation for this whole topic of concurrency
is the book C++ Concurrency in Action by Anthony Williams (see [Williams:C++Conc]).
การแปล กรุณารอสักครู่..