I want to learn multi-thread, then I copy some code from internet.
findOdd / findEven function means adding all odd/even number together
//using thread
int main(){
ull start=0, end =1900000000;
auto startTime =high_resolution_clock::now();
std::thread t1(findEven,start,end);
std::thread t2(findOdd,start,end);
t1.join();
t2.join();
auto stopTime=high_resolution_clock::now();
auto duration=duration_cast<microseconds>(stopTime-startTime);
cout<<OddSum<<"\n";
cout<<EvenSum<<"\n";
cout<<"duration: "<<float(duration.count())/1000000;
return 0;
}
// using thread end (It takes 16 second)
if not using thread,
findOdd(start,end);
findEven(start,end);
just use these code to replace. (It takes 2.6 seconds)
may I know why?