Introduction:
A thread is a smallest unit of program execution. These threads can run in parallel and can increase efficiency and decrease execution time of a program. A thread reside inside of a process.
In these multi threading chapters, it deals with C++ threading concepts rather than OS threading concepts.
Every process by default will have a thread, i.e main() thread. Hence all the other threads needs to be created inside the main thread.
A thread is also known as light weight process.
Generic example for threads:
1. When you open multiple tabs in browser, multiple threads will be created.
2. In word editor, there will be one thread to check the spelling, another thread to save the progress.
3. In IDE like Visual Studio, there will be multiple threads for code auto completion, error detection etc.
Header required for Multi threading
#include <thread>
How many ways are there to create a thread?
There are 5 different ways to create a thread:
1. Function Pointers
2. Lambda Functions
3. Functors
4. Member Functions
5. Static Member functions
Let us understand how thread helps in faster execution of a program with the help of an example.
Program to find the sum of even and odd numbers from 1 to 1900000000
#include <iostream>
#include <thread>
#include <chrono>
// for more tutorial in C++ visit www.prodevelopertutorial.com
using namespace std;
using namespace std::chrono;
using ull = unsigned long long;
ull evenSum = 0;
ull oddSum = 0;
void getEvenSum()
{
for (ull i = 1; i <= 1900000000; i++)
{
if ((i & 1) == 0)
{
evenSum += i;
}
}
}
void getOddSum()
{
for (ull i = 1; i <= 1900000000; i++)
{
if ((i & 1) == 1)
{
oddSum += i;
}
}
}
int main()
{
//start the time
auto start = high_resolution_clock::now();
getEvenSum();
getOddSum();
//stop the time after execution
auto stop = high_resolution_clock::now();
cout<<"Even sum = "<<evenSum<<endl;
cout<<"Odd sum = "<<oddSum<<endl;
auto duration = duration_cast<microseconds>(stop - start);
cout<<"Total duration of the execution of the program is = "<<duration.count()/1000000 << " sec"<<endl;
return 0;
}
Output:
Even sum = 902500000950000000
Odd sum = 902500000000000000
Total duration of the execution of the program is = 15 sec
The above program, we are calculating the total even sum and odd sum. We know that, according to the program written, first “getEvenSum()” will be executed and then “getOddSum()” will be executed.
The total time taken is 11686026 microseconds.
Now lets see the same example with threads.
#include <iostream>
#include <thread>
#include <chrono>
// for more tutorial in C++ visit www.prodevelopertutorial.com
using namespace std;
using namespace std::chrono;
using ull = unsigned long long;
ull evenSum = 0;
ull oddSum = 0;
void getEvenSum()
{
for (ull i = 1; i <= 1900000000; i++)
{
if ((i & 1) == 0)
{
evenSum += i;
}
}
}
void getOddSum()
{
for (ull i = 1; i <= 1900000000; i++)
{
if ((i & 1) == 1)
{
oddSum += i;
}
}
}
int main()
{
//start the time
auto start = high_resolution_clock::now();
// std::thread is used to create a new thread.
// we have created a function pointer type of thread below.
// because we are sending the function pointer as the parameter to the thread.
std::thread t1(getEvenSum);
std::thread t2(getOddSum);
// .join() should be called to join the new threads to the main thread.
// if we dont join, if the main threads exit before the completion of t1 and t2 threads, then there is no way of
// getting those threads back.
t1.join();
t2.join();
//stop the time after execution
auto stop = high_resolution_clock::now();
cout<<"Even sum = "<<evenSum<<endl;
cout<<"Odd sum = "<<oddSum<<endl;
auto duration = duration_cast<microseconds>(stop - start);
cout<<"Total duration of the execution of the program is = "<<(duration.count()/1000000) << " sec"<<endl;
return 0;
}
Output:
Even sum = 902500000950000000
Odd sum = 902500000000000000
Total duration of the execution of the program is = 8 sec
So as you can see, by creating multiple threads, you can execute your program faster.