Process:
It can be simply understood as an executable program
Thread:
A process can contain multiple threads, and there must be a main thread.
Differences and connections between processes and threads:
1) The process is the smallest resource allocation unit and the thread is the smallest program execution unit. Every time a new process is established, the system needs to allocate the corresponding address space for it, while the thread shares the data segment, which is the same address space. It costs much less for the CPU to switch a thread than to switch a process.
2) The communication between processes is not very convenient. For example, it has experienced network programming technologies such as SOCKET. Because threads share data segments, the communication between threads is very convenient.
3) Multi process programs are robust and stable. As long as one program crashes, the whole program crashes, while one program crashes, other programs can still run independently.
Several ways to create threads
1, Simple thread function
# include<iostream> # Include < thread > / / introduce header files contained in multiple threads using namespace std; //Thread function void print_hello() { cout << "My thread started " << endl; //...... cout << "My thread is over " << endl; } int main() { std::thread study_1(print_hello);//Create a new thread and start execution. The main thread continues to execute downward and also starts to execute the contents of the thread function cout << "This is the main thread " << endl; study_1.join();//After the main thread executes here, it needs to wait for the thread function to finish executing, }
2, Take the class object as the thread function, overload (), and initialize the class object as the thread function
# include<iostream> # Include < thread > / / introduce header files contained in multiple threads using namespace std; //Thread function void print_hello() { cout << "My thread started " << endl; //...... cout << "My thread is over " << endl; } class Thread_function { public: void operator()() { cout << "Create a thread " << this_thread::get_id() << endl; cout << "Thread function execution completed " << endl; } }; int main() { std::thread study_1(print_hello);//At the same time, a new thread in the main thread is created and executed //Initialize class object Thread_function get_id; std::thread study_2(get_id); study_1.join();//After the main thread executes here, it needs to wait for the thread function to finish executing. study_2.join();//After the main thread executes here, it needs to wait for the thread function to finish executing. cout << "This is the main thread " << endl; }
3, Take a function in a class as a thread function. After initializing the class object, the first parameter is the thread function in the object, and the second function is the name of the class object, which needs to be preceded by &.
# include<iostream> # Include < thread > / / introduce header files contained in multiple threads using namespace std; //Thread function void print_hello() { cout << "My thread started " << endl; //...... cout << "My thread is over " << endl; } class Thread_function { public: void operator()() { cout << "Create a thread " << this_thread::get_id() << endl; cout << "Thread function execution completed " << endl; } void Thread_function_1(); }; void Thread_function::Thread_function_1() { cout << "Create a thread by using the function in the class as a thread function " << this_thread::get_id() <<endl; cout << "Class as thread function "<< endl; } int main() { std::thread study_1(print_hello);//Create a new thread and start execution. The main thread continues to execute downward and also starts to execute the contents of the thread function //Initialize class object Thread_function get_id; std::thread study_2(get_id); std::thread study_3(&Thread_function::Thread_function_1,&get_id); study_1.join();//After the main thread executes here, it needs to wait for the thread function to finish executing. study_2.join();//After the main thread executes here, it needs to wait for the thread function to finish executing. study_3.join();//After the main thread executes here, you need to wait for the thread function to finish executing, cout << "This is the main thread " << endl; }
Pass parameters to thread functions
1) Pass parameters to the thread function. You can add function parameters after the thread function when initializing the object
2) The default thread function parameter transfer is value transfer. If you want to transfer a reference, you need to add ref() to the parameter transfer, and the thread function should add a reference symbol.
3) The const qualifier needs to be added in front of the ordinary reference, because the source code initialization is an R-value reference, so const should be added.
4) When the class member function pointer is used as a thread function, you can directly change the data of the object, which has the same meaning as the reference. If you need parameters, add them after the object.
void changebyref( int& a) { a++; } void changebyref( int& a) { a++; } int main() { int num = 1; std::thread function1(changebyref, ref(num)); cout << "nums is " << num << endl; function1.join(); cout << "nums is " << num << endl; }
Class member function pointer as thread function
class Thread_function { int a; int b; public: void set_a(int a_) { a = a_; } void set_b(int b_) { b = b_; } void sum() { cout << "Value is " << a + b << endl; } Thread_function(int a_ = 0, int b_ = 0) :a(a_), b(b_) {} }; int main() { Thread_function rui; std::thread fun_1(&Thread_function::set_a, &rui,20); std::thread fun_2(&Thread_function::set_b, &rui,20); fun_1.join(); fun_2.join(); rui.sum(); }
Batch creation of threads and transfer of thread ownership
void count1(int a) { cout << "At this time a yes " << a << endl; } int main() { vector<thread> More_thread; for (int i = 0; i <= 9; i++) { More_thread.push_back(std::thread(count1,i)); } for (int i = 0; i < More_thread.size(); i++) { More_thread[i].join(); } }
Transfer thread ownership using Move