C++11, which encapsulates thread's multithreaded class, makes multithreading easier to use.
The principle of multithreading I do not repeat, you can refer to the operating system and other reference books.
Multithreaded code can maximize the use of computer performance resources, improve the efficiency of code, is a common optimization method.
I am not a C + + God, beginner stage rookie just, a lot of questions I still do not understand the principle, write this blog reason, is also record their learning experience and ideas, for their own future thinking.
Start with a simple question, how to write a multithreaded C + + code?
#include <iostream>#include<thread>void fun (int a) { a+ +;} int Main () { int a=0; Std::thread T (fun,a); Create a thread t,t call function Fun,a as a fun parameter, also write to the thread's constructor;
T.join (); Start Thread T, and block the main thread, wait until the thread T runs to finish, and then continue to run the main thread; std::cout<<a<<Std::endl;}
The above code is the simplest multithreaded code, calling the thread class, and using the thread's constructor to create a thread T,thread class constructor overloads a lot, which will continue to be said later.
Here's a look at the two member functions in the thread class, join () and detach (). The two members function as comments on the code above, starting the newly generated thread, but the difference is that the join () function blocks the main thread by starting the child threads, and then continues running the main thread when the child thread finishes running. By contrast, the detach () function is to start a child thread and separate the child thread from the main thread, and the child thread and the main thread each run each, although two threads are blocked at the level of the operating system because of the reason for the shared memory pool, but at the code level, two threads do not exist who is blocking who, It is likely that the main thread is already running and the child thread is still running.
Next, let's talk about how member functions in the class initialize the constructor of the thread class.
For a class member function, we need to give the address of the class object:
#include <iostream> #include <thread> class a{ public : void Fun (int a,int b) {std::cout << this is A thread! " <<a<<STD::ENDL; }}; int main () { int k= 0 ; A; Std::thread T ( &a::fun,a,k,k+1
std::thread T (&a::fun,a,k,k+1); This place can be seen that the construction of the thread class is overloaded with member functions,std::thread t (function (member function) address, object address, parameter 1 of the member function, parameter 2, Parameter 3 ...).
Instead of a non-member function, the member function needs to give the address of the class instantiation object, and if the thread is constructed in a member function of the same category, it can be replaced by the This keyword directly.
In fact, when I write the multi-threaded code of the member function, I find that the parameters of the member function can be passed too much to fail the constructor overload of the thread class, and I test that the member function can only pass 4 parameters at most, and that the constructor of the Std::thread class can only overload 6 parameters.
This, I did not find the relevant documents to be confirmed, just write code when the member function passed too many parameters, will be compiled do not pass, accidentally found this point, the exact right, I am not very sure.
Secondly, we have to talk about locking and unlocking problems.
Because every thread we create is a shared memory pool within a single process, it can be confusing to read and write data.
Then, let's talk about the volatile keyword.
Finally, let's talk about the use of join () and detach ().
C++11 thread Multi-threading learning experience and problems