Multithreading concurrency has always been something I want to know. Now it's also an introduction. Write my summary.
This is a simple code I wrote. Here's the running result. After reading the introduction to multithreading:
#include <thread> #include <iostream> #include <vector> #include<unistd.h> using namespace std; class Calculation{ public: explicit Calculation(long a):num(a){}; ~Calculation(){}; inline void add1(){ for(int i =0; i<1000;i++) { num++; usleep(1000); } }; inline void add10(){ for(int i =0; i<1000;i++) { // num++; num+=10; usleep(1000); } }; inline void add100(){ for(int i =0; i<1000;i++) { // num++; num+=100; usleep(1000); } }; inline void add1000(){ for(int i =0; i<1000;i++) { // num++; num+=1000; usleep(1000); } }; inline long GetNum(){return num;}; private: long num; }; int main() { std::vector<std::thread> threads; bool ifMulti = false; // for(int i = 0; i < 5; ++i){ if(!ifMulti) { Calculation test1(1); Calculation test10(1); Calculation test100(1); Calculation test1000(1); std::chrono::high_resolution_clock::time_point tnow = std::chrono::high_resolution_clock::now(); test1.add1(); cout <<"add 1 step :" <<test1.GetNum()<<endl; test10.add10(); cout <<"add 10 step :" <<test10.GetNum()<<endl; test100.add100(); cout <<"add 100 step :" <<test100.GetNum()<<endl; test1000.add1000(); cout <<"add 1000 step :" <<test1000.GetNum()<<endl; std::chrono::high_resolution_clock::time_point tpost = std::chrono::high_resolution_clock::now(); std::cout << "no multi cost time: " << std::chrono::duration_cast<std::chrono::duration<double>>(tpost - tnow).count() * 1000 << " ms" << std::endl; } if(ifMulti) { std::chrono::high_resolution_clock::time_point tnow = std::chrono::high_resolution_clock::now(); threads.push_back(std::thread( [&](){ Calculation test1(1); test1.add1(); cout <<"add 1 step :" <<test1.GetNum()<<endl; } )); threads.push_back(std::thread( [&](){ Calculation test10(1); test10.add10(); cout <<"add 10 step :" <<test10.GetNum()<<endl; } )); threads.push_back(std::thread( [&](){ Calculation test100(1); test100.add100(); cout <<"add 100 step :" <<test100.GetNum()<<endl; } )); threads.push_back(std::thread( [&](){ Calculation test1000(1); test1000.add1000(); cout << "add 1000 step :"<<test1000.GetNum()<<endl; } )); // } for(auto& thread : threads){ thread.join(); } // std::cout << test1.GetNum() <<std::endl; // std::cout<<"Main Thread"<<"\t"<<std::this_thread::get_id()<<std::endl; std::chrono::high_resolution_clock::time_point tpost = std::chrono::high_resolution_clock::now(); std::cout << "muti_threads cost time: " << std::chrono::duration_cast<std::chrono::duration<double>>(tpost - tnow).count() * 1000 << " ms" << std::endl; } return 0; }
When ifMulti is false, run the operations of four objects in turn, and the results are as follows:
add 1 step :1001 add 10 step :10001 add 100 step :100001 add 1000 step :1000001 no multi cost time: 4662.88 ms
When ifMulti is true, the operations of four objects are run in parallel, and the results are as follows:
add 1 step :1001 add 10 step :10001 add 1000 step :1000001 add 100 step :100001 muti_threads cost time: 1146.31 ms
Note that the original function operation time cannot be too short, or multi-threaded has no efficiency advantage, because there is a time cost when switching threads.