C++11 introduces a new line threading that includes tools to start and manage threads, and provides a way to synchronize (mutex, lock, and atomic variables), and I will try to introduce you to this new line threading.
If you want to compile the code in this article, you need at least one compiler that supports C++11, I'm using GCC 4.6.1, and I need to use-c++0x or-c++11 parameters to enable c++11 support.
Start thread
Starting a thread in c++11 is very simple, you can use Std:thread to create a thread instance, the creation will start automatically, only need to pass a pointer to execute the function, please see the following Hello World code:
#include <thread>
#include <iostream>
void Hello () {
std::cout << "Hello from Thread" << Std::endl;
}
int main () {
std::thread t1 (hello);
T1.join ();
return 0;
}
All thread-related methods are defined in the thread header file, and it is interesting that we call the join () function in the above code to force the main thread to wait for execution to end before exiting. If you do not write join () This line of code, the possible result is to print the Hello from thread and a new line, or there may not be a new row. Because the main thread may be returned before the thread has finished executing.
Thread identification
Each thread has a unique ID to identify a different thread, and the Std:thread class has a get_id () method that returns a unique number for the corresponding thread, and you can access the current thread instance through Std::this_thread, and the following example shows how to use this ID:
#include <thread>
#include <iostream>
#include <vector>
void Hello () {
std::cout << "Hello from Thread" << std::this_thread::get_id () << Std::endl;
}
int main () {
std::vector<std::thread> threads;
for (int i = 0; i < 5; ++i) {
threads.push_back (hello);
}
for (auto& thread:threads) {
thread.join ();
}
return 0;
}
Start each thread in turn, and then save it in a vector container, and the results of the program execution are unpredictable, such as:
Hello from thread 140276650997504
hello to thread 140276667782912
Hello from thread 140276659390208
Hello from thread 140276642604800
Hello from thread 140276676175616
It may also be:
Hello from thread hello from thread hello to thread 139810974787328Hello from thread 139810983180032Hello from Thread
139810966394624
139810991572736
139810958001920
or other results, because the execution of multiple threads is interleaved. You have absolutely no way to control the order in which threads are executed (otherwise, why do they need threads?) )
When the thread is going to execute the code a little bit, you don't have to create a function specifically for it, you can use lambda to define the code to execute, so the first example we can rewrite as:
#include <thread>
#include <iostream>
#include <vector>
int main () {
std::vector <std::thread> Threads;
for (int i = 0; i < 5; ++i) {
threads.push_back (Std::thread ([] () {
std::cout << "Hello from Thread" <& Lt std::this_thread::get_id () << Std::endl;
});
for (auto& thread:threads) {
thread.join ();
}
return 0;
}
Here we use a lambda expression to replace the function pointer, and the result is the same.