I am new to multithreaded programming in C++. I wrote a simple piece of code, which I will paste below. When run in two threads the code finishes barely faster than when run in single threaded. I have come across other similar questions, however they are different as I have no shared resources which both threads will need to access:
The code is below:
#include <iostream>
#include <thread>
#include <mutex>
#include <chrono>
using namespace std;
typedef unsigned long long ull;
ull oddSum = 0;
ull evenSum = 0;
void addOdd(){
for(int i = 1; i <= 1999999999; i++){
if(i % 2 == 1)
oddSum += i;
}
}
void addEven(){
for(int i = 1; i <= 1999999999; i++){
if(i % 2 == 0)
evenSum += i;
}
}
int main(){
auto startTime = std::chrono::high_resolution_clock::now();
//Two threads
std::thread t1(addEven); //launch the two threads to run
std::thread t2(addOdd);
t1.join();
t2.join(); //wait for both to finish
//One thread
//addEven();
//addOdd();
auto stopTime = std::chrono::high_resolution_clock::now();
auto elapsed = std::chrono::duration_cast<std::chrono::microseconds>(stopTime - startTime);
cout << "Odd Sum: " << oddSum << endl;
cout << "Even Sum: " << evenSum << endl;
cout << elapsed.count()/(double)1000000 << endl;
return 0;
}
When I run with a single thread, average on 10 runs is around 7.3 seconds.
When I run with two threads, average on 10 runs is around 6.8 seconds.
Since the vast majority of the time is taken by the loops in the functions, I would believe that running two threads in parallel, each with a single function, would halve the time it takes to run.
Note 1: I know the time cannot possibly be properly halved, a more educated guess might be a run time of up to 5 secs in two threads. I understand the creation of the thread objects has its own overhead.
Note 2: Maybe I am missing something, but the threads are not accessing any shared location between the two.
Any idea is welcome. I am familiar with the theory behind concurrent programming, and I am now just starting to gain some hands-on experience. I have an intel i7 with 4 cores.