多线程在进行复杂运算时能够大量节约时间
提醒自己能够在多线程运算的时候一定要充分利用
能够省下2/3的时间
测试代码
#include<vector>
#include <iostream>
#include <thread>
using namespace cv;
using namespace std;
int result1 = 0 ;
bool thread_flag1 = false;
bool thread_flag2 = false;
bool thread_flag3 = false;
bool thread_flag4 = false;void add_fn (int time)
{int result = 0;for (int i = 0 ;i < 10000000 ;i++){result += i ;} // cout << "the " << time <<"time " << result <<endl;if (time == 1 ) thread_flag1 = true;else if (time == 2 ) thread_flag2 = true;else if (time == 3 ) thread_flag3 = true;else thread_flag4 = true;// return result ;
}
void check_thread()
{auto beforeTime = std::chrono::steady_clock::now();while(1){if(thread_flag1&&thread_flag2&&thread_flag3&&thread_flag4){auto afterTime = std::chrono::steady_clock::now();double duration_microsecond = std::chrono::duration<double, std::micro>(afterTime - beforeTime).count();cout << "threads take times"<< duration_microsecond <<"microsecond" <<endl;break;}}}int main()
{thread check(check_thread);check.detach();for (int i = 0;i < 4 ; i++){std::thread t(add_fn , i);t.detach();}auto beforeTime = std::chrono::steady_clock::now();for (int i = 0;i < 4 ; i++){add_fn(i);}auto afterTime = std::chrono::steady_clock::now();double duration_microsecond = std::chrono::duration<double, std::micro>(afterTime - beforeTime).count();cout << "all take times"<< duration_microsecond <<"microsecond" <<endl;while(1){}
}