mirror of
https://github.com/changkun/modern-cpp-tutorial.git
synced 2025-12-17 04:34:40 +03:00
book: revise usage of "mutex" and "instantiation" (#292)
This commit is contained in:
@@ -31,7 +31,7 @@ int main() {
|
||||
We have already learned the basics of concurrency technology in the operating system, or the database, and `mutex` is one of the cores.
|
||||
C++11 introduces a class related to `mutex`, with all related functions in the `<mutex>` header file.
|
||||
|
||||
`std::mutex` is the most basic `mutex` class in C++11, and you can create a mutex by instantiating `std::mutex`.
|
||||
`std::mutex` is the most basic mutex class in C++11, and a mutex can be created by constructing a `std::mutex` object.
|
||||
It can be locked by its member function `lock()`, and `unlock()` can be unlocked.
|
||||
But in the process of actually writing the code, it is best not to directly call the member function,
|
||||
Because calling member functions, you need to call `unlock()` at the exit of each critical section, and of course, exceptions.
|
||||
@@ -160,7 +160,7 @@ After encapsulating the target to be called, you can use `get_future()` to get a
|
||||
The condition variable `std::condition_variable` was born to solve the deadlock and was introduced when the mutex operation was not enough.
|
||||
For example, a thread may need to wait for a condition to be true to continue execution.
|
||||
A dead wait loop can cause all other threads to fail to enter the critical section so that when the condition is true, a deadlock occurs.
|
||||
Therefore, the `condition_variable` instance is created primarily to wake up the waiting thread and avoid deadlocks.
|
||||
Therefore, the `condition_variable` object is created primarily to wake up the waiting thread and avoid deadlocks.
|
||||
`notify_one()` of `std::condition_variable` is used to wake up a thread;
|
||||
`notify_all()` is to notify all threads. Below is an example of a producer and consumer model:
|
||||
|
||||
@@ -276,8 +276,8 @@ This is a very strong set of synchronization conditions, in other words when it
|
||||
This seems too harsh for a variable that requires only atomic operations (no intermediate state).
|
||||
|
||||
The research on synchronization conditions has a very long history, and we will not go into details here. Readers should understand that under the modern CPU architecture, atomic operations at the CPU instruction level are provided.
|
||||
Therefore, in the C++11 multi-threaded shared variable reading and writing, the introduction of the `std::atomic` template, so that we instantiate an atomic type, will be an
|
||||
Atomic type read and write operations are minimized from a set of instructions to a single CPU instruction. E.g:
|
||||
Therefore, the `std::atomic` template is introduced in C++11 for the topic of multi-threaded shared variable reading and writing, which enables us to instantiate atomic types,
|
||||
and minimize an atomic read or write operation from a set of instructions to a single CPU instruction. E.g:
|
||||
|
||||
```cpp
|
||||
std::atomic<int> counter;
|
||||
|
||||
Reference in New Issue
Block a user