Difference between thread and process:
Simply put, a process is the running thread of an exe. The thread is the execution of a code fragment in the process and the smallest unit of thread memory allocation
Parallel:
There can be multiple threads in a process, and the operation between threads can be parallel
How to synchronize or message transfer between multiple threads:
Threads in the same computer can be synchronized through global variables and memory management. socket communication can be used for thread synchronization between different computers
Creation of C + + multithreading:
Contains the C + + thread class thread
- Normal function creation thread
- Creating threads from classes and objects
- Lambda expression creation thread
- Creating threads with parameters
- Create thread with smart pointer
- Creating threads through class member functions
Thread calling methods and precautions
1.join(); Method calls the thread, then blocks the main thread and waits for the sub thread to execute before the main thread executes
2.detch(); The method calling thread is parallel to the main thread and does not block the main thread, but it has nothing to do with whether the main thread has completed execution
3. Judge whether the thread has called the joinable() method of the thread object
4. The method for obtaining the ID of the thread can use the static method this in the thread function_ thread::get_ Id() to get the thread ID
5. To create a thread with parameters, you need to use the std::ref() method to modify the parameters
6. The pointer can only be moved with move, but it should be noted that the memory address of the pointer will be cleared after moving.
Common function creation thread * ↓
#include <iostream> #include <thread> using namespace std; void print() { cout << "Normal function thread\t ID:"<<this_thread::get_id() << endl; return; } int main1() { thread thred(print); if (thred.joinable()) { cout << "The main thread has called join function" <<endl; } else { cout << "Main thread not called join Function, calling" << endl; thred.join(); } system("pause"); return 0; }
Create threads through classes and objects * * * * ↓**
#include <iostream> #include <thread> using namespace std; class XX { public: //Use the operator method to overload () void operator()() { cout << "Class object thread\t ID:" << this_thread::get_id() << endl; } }; int main2() { //To call directly using the class name, you need to add another layer (), otherwise an error will be reported thread thred1((XX())); thred1.join(); //Call through class object XX xx; thread thred2((xx)); thred2.join(); system("pause"); return 0; }
Lambda expression creation thread * * * * ↓**
#include <iostream> #include <thread> using namespace std; int main3() { // Lambda expression [] () - > {} thread thred1([]() {1 > 2 ? 1 : 2; }); thred1.join(); system("pause"); return 0; }
Thread creation with parameters * * * * ↓**
#include <iostream> #include <thread> using namespace std; void print4(int &number) { number = 10086; cout << "number:" <<number <<endl; } int main4() { int numb = 1000; //You need to use the std::ref() method to decorate the parameters thread thred1(print4,std::ref(numb)); thred1.join(); cout << "number:" << numb << endl; system("pause"); return 0; }
Thread creation with smart pointer * * * * ↓**
#include <iostream> #include <thread> using namespace std; void printf5( unique_ptr<int> ptr) { cout << "&ptr:" << &ptr << endl; cout << "ptr:" << *ptr << endl; } int main5() { unique_ptr<int> ptr(new int(1000)); //The pointer can only be moved with move, but it should be noted that the memory address of the pointer will be cleared after moving. thread thred1(printf5, move(ptr)); thred1.join(); system("pause"); return 0; }
Create thread through class member function * * * * ↓**
#include <iostream> #include <thread> using namespace std; class XXX { public: void pornt(int &number) { cout << "number:" << number << endl; } }; int main() { int numb = 10086; XXX xx; //When creating a thread with a class function, you need to add a parameter as a function object thread thred(&XXX::pornt,xx, std::ref(numb)); thred.join(); system("pause"); return 0; }
When multiple threads access the same memory space, it doesn't matter if all threads are read-only. However, if these threads modify the memory space, they need to add a mutex to ensure that when one thread accesses the memory address, it will not be modified by other threads at the same time, resulting in memory leakage.