0% found this document useful (0 votes)
5 views3 pages

C++ Threading Techniques Explained

The document discusses C++ threading, explaining that threads are lightweight processes that can be created using various methods such as function pointers, lambda functions, functors, and member functions. It provides an example of calculating the sum of odd and even numbers from 1 to 10 billion using threads. Additionally, it includes code snippets demonstrating different ways to create threads in C++.

Uploaded by

yatharthst616
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views3 pages

C++ Threading Techniques Explained

The document discusses C++ threading, explaining that threads are lightweight processes that can be created using various methods such as function pointers, lambda functions, functors, and member functions. It provides an example of calculating the sum of odd and even numbers from 1 to 10 billion using threads. Additionally, it includes code snippets demonstrating different ways to create threads in C++.

Uploaded by

yatharthst616
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

C++ Threading

In every program main is the main thread.


Thread is a light weight [Link] divide the process into multiple threads.
For example:
1)The browser has multiple tabs as threads.
2)MS Word must be using multiple thread for formatting text and for formatting
inputs.
3)VS Code for auto completeing the code.
Ways to create threads in CPP
1)Function Pointers
2)Lambda Functions
3)Functors
4)Member Functions
5)Static Member Functions

//Requirement
Find the addition of all odd numbers from 1 to 10000000000 and all even numbers
from 1 to 10000000000.

#include<iostream>
#include<thread>
#include<chrono>
#include<algorithm>
using namespace std;
using namespace std::chrono;
typedef unsigned long long ull;

ull OddSum=0;
ull EvenSum=0;

void findEven(ull start,ull end)


{
for(ull i=start;i<=end;i++)
{
if((i & 1)==0)
{
EvenSum+=1;
}
}
}
void findOdd(ull start,ull end)
{
for(ull i=start;i<=end;i++)
{
if((i & 1)==1)
{
OddSum+=1;
}
}
}

int main()
{
ull start=0,end=10000000000;
findOdd(start,end);
findEven(start,end);
std::thread t1(findEven,start,end);
std::thread t1(findOdd,start,end);
[Link]();
[Link]();
cout<<"OddSum"<<Oddsum<<endl;
cout<<"EvenSum"<<EvenSum<<endl;
}

##############How to Create Threads In CPP###############


1)Function Pointer
Note:If we create multiple threads in same time,there is no guarantee which one
will start first.

void fun(int x)
{
while(x-->0)
{
cout<<x<<endl;
}
};
int main()
{
std::thread t1(fun(),11);
std::thread t2(fun(),10);
[Link]();
[Link]();
return 0;
}

################Lambda Function##################
2)Lambda Function
int main()
{
// We can directly inject lambda at thread creation time.
auto fun=[](int x)
{
while(x-->0)
{
cout<<x<<endl;
}
};
std::thread t(fun,10);
[Link]();

return 0;
}
#################Functor Function##################
3)Functor(Function Object)
class Base
{
public:
void operator()(int x)
{
while(x-->0)
{
cout<<x<<endl;
}
}
};
int main()
{
std::thread t((Base()),10);
[Link]();
return 0;
}
###################Non Static Member Function##################
class Base
{
public:
void run(int x)
{
while(x-->0)
{
cout<<x<<endl;
}
}
};
int main()
{
Base b;
std::thread t(&Base::run,&b,10);
[Link]();
return 0;
}

##################Static Member Function###############################


5)
class Base
{
public:
static void run(int x)
{
while(x-->0)
{
cout<<x<<endl;
}
}
};

int main()
{
std::thread t(&Base::run,10);
[Link]();
return 0;
}

You might also like