Chapter 1
Why use concurrency, there are two main reasons to use concurrency in an application: separation of con-cerns and performance. (This sentence is brilliant :)
Me: separation of implementation logic, and then using the "State Migration table" message-driven approach, is very convenient in APP design.
When should I use concurrency?
There is a price to use the thread, because the thread needs to occupy the resource (stack), context switch. Also, multithreading is more difficult to understand and error-prone than a single-threaded program. Weigh the gains and losses.
Multithreading in C ++:
In C ++ 98, there were no multithreading, mainly using system APIs or third-party thread libraries (boost, MFC, Ace. The biggest problem is that C ++ itself does not have a multi-threaded memory model. Especially when you want to write high-performance, cross-platform multi-threaded programs, the performance of different compilers and platforms may cause some problems.
In the C ++ 11 standard, the multi-threaded Memory Model and the standard library have a set of threads and synchronization-related libraries.
Performance of the C ++ 11 thread Library:
Me: This is probably one of the top concerns of C/C ++ programmers ...... (This is generally more important than ease of use ......)
The design goal of the standard library is "No or almost no performance loss compared to the underlying calling when abstraction is implemented"
The author believes that programmers will hardly encounter performance bottlenecks because they use standard thread libraries instead of the original multi-threaded libraries. The implementation of some thread library features will cause slight performance loss. But it does not matter to the program. The author believes that even if the performance bottleneck occurs on the thread Library (for example, synchronization locks) during profile, the optimization should generally be the program design (framework or structure ), instead of replacing the standard thread library with other implementations.
Chapter 2: basic operations of threads
The most important Library: STD: thread header file <thread>
void do_some_work();std::thread my_thread(do_some_work);
Or the function object can also:
class background_task{public: void operator()() const { do_something(); do_something_else(); }};background_task f;std::thread my_thread(f);
Note:
STD: thread my_thread (background_task ());
This method is not feasible, because the compiler will assume that you are declaring a function named my_thread and a return value (STD: thread) for a parameter)
If you really want to use a temporary variable to generate a thread, you can use one of the following two methods:
std::thread my_thread((background_task())); std::thread my_thread{background_task()};
Or lambda expressions:
std::thread my_thread([]( do_something(); do_something_else();});
Pay attention to the thread lifetime. Do not allow the thread to access invalid objects.
Listing2.1 an example of getting started
2.1.3 and 2.1.4
Join and detach Functions
Join makes you wait for the end of a thread
Detach allows a thread to run in the background. (After detach, you cannot join it again)
After a thread object is generated, you must call one of the two. Otherwise, in the STD: thread destructor, the entire program ~!??
Try it !~
2.2 pass parameters to the thread function when creating a thread
void f(int i,std::string const& s);std::thread t(f, 3,”hello”);
The method for passing parameters is very simple. In the constructor of the thread object, you only need to pass parameters according to the parameter list of the thread function (or function object.
However, the author points out several possible "Misunderstandings" because the parameters of the STD: thread constructor are not exactly the same as those of the thread function. Therefore, an "error" may occur during implicite convert and pass by reference ". In addition, pay attention to the "lifecycle" of the input parameter ". (Refer to bind)
(This section needs to be read again !)
2.3 transfer thread object
Specified resource-owning types in the C ++ standard library such as STD: ifstreamand STD: unique_ptrare movablebut not copyable, and STD: thread is one of them.
STD: The thread supports move and can be placed in the container.
2.4
std::thread::hardware_concurrency()
Returns the real "number of parallel rows" of the running platform ". But it may also be 0.
This function enables the program to obtain the parallel capabilities of the platform at runtime and select corresponding policies.
2.5
Thread_id is used to identify a thread.