What are the ways to use C++ primary threads?
This article mainly explains "what are the methods for the use of primary threads in C++". The content of the explanation in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought slowly and deeply. Let's study and learn what are the methods for the use of primary threads in C++.
1 thread start
In std::thread 11, the startup of a thread is ultimately a construction of the object of the thread.
The categories of thread constructs are as follows:
1.1 Thread function has no parameters and no return value
This class can be said to be the simplest thread to start. The function does not need to pass parameters or return the result of the function execution. After the execution is completed, the thread automatically exits.
It looks like:
Void FunDoingNothing (); std::thread (FunDoingNothing)
When writing code, you need to add a header file to make it easier for the compiler to handle thread objects correctly.
1.2 Thread functions have parameters and no return values
In thread 11, variable parameters are used in the constructor of the thread, which makes it possible to customize the incoming parameters when constructing the Cobb object.
The constructor is defined as follows:
Template explicit thread (fame & f, Args&&...) Args)
In practical use, a thread function can be defined as follows when it has parameters:
Void printMsg (int a, int b) {cout