You are on page 1of 22

Module 2

Concurrent Programming
Module 1
Subtopic 1
Concurrency in C++
• To install appropriate IDE and build tools for C++
• To perform various ways of specifying code to run on a
new thread
• To perform simple thread management
• To implement data sharing among threads
• To determine problems associated with sharing data
between threads
https://en.cppreference.com/w/cpp/thread
#include <iostream>
#include <thread>

using namespace std;

int main() {

cout << "Hello World from main()" << endl;

cout << this_thread::get_id() << endl;

}
int main()
#include <iostream>
{
#include <thread>
cout << "Hello World from main()" << endl;
using namespace std; thread t1(hellofunction);

HelloObject ho;
void hellofunction(){ thread t2(&HelloObject::objectfunction, &ho);
cout << "Hello from function..." << endl; //runs HelloObject::objectfunction() on object ho
}
t1.join();
class HelloObject {
public:
t2.join();
void objectfunction();
};
}
void HelloObject::objectfunction(){
cout << "Hello from object function" <<endl;
}
• When a thread object goes out of scope and it is in
joinable state, the program is terminated.
• join() - waits for the thread to finish its execution

• detach() - permits the thread to execute independently


from the thread handle
int main()
{
cout << "Hello World from main()" << endl;
thread t1(hellofunction);

HelloObject ho;
thread t2(&HelloObject::objectfunction, &ho); //runs HelloObject::objectfunction() on object
ho

t1.join()
t2.join()

cout << "\nmain() program ends..." << endl;

}
int main()
{
cout << "Hello World from main()" << endl;
thread t1(hellofunction);

HelloObject ho;
thread t2(&HelloObject::objectfunction, &ho); //runs HelloObject::objectfunction() on object ho

t1.detach();
t2.detach();

cout << "\nmain() program ends..." << endl;

}
#include <iostream>
#include <thread>
int main()
{
using namespace std; HelloObject ho;
thread t1(ho);
t1.join();
class HelloObject {
public: cout << "\nmain() program ends..." << endl;
void operator()(){
}
cout << "Hello object method thread..." << endl;

}
};
#include <iostream> int main()
#include <thread> {
#include <string>
cout << "Hello World from main()" << endl;
using namespace std; thread t1(hellofunction,5, "FEUTECH");

HelloObject ho;
void hellofunction(int n, string str){ thread t2(&HelloObject::objectfunction, &ho, 5, "FEUTECH");
t1.join();
for(int i=0; i<n; i++)
t2.join();
cout << "Hello from function... " << " " << str << endl;
}
cout << "\nmain() program ends..." << endl;
class HelloObject {
public:
void objectfunction(int n, string str); }
};

void HelloObject::objectfunction(int n, string str){

for(int i=0; i<n; i++)


cout << "Hello from object function" << " " << str << endl;
}
https://en.cppreference.com/w/cpp/thread
void hellofunction(int n, string str){

for(int i=0; i<n; i++){


cout << "Hello from function... " << " " << str << " " << this_thread::get_id << endl;
this_thread::sleep_for(2000ms);
}
}
#include <iostream>
#include <thread>
#include <string>
#include <chrono>

class HelloObject {
public:
void objectfunction(int n, string str);
};

void HelloObject::objectfunction(int n, string str){


this_thread::sleep_until(chrono::system_clock::now() + 5000ms );
for(int i=0; i<n; i++)
cout << "Hello from object function" << " " << str << endl;
}
One of the key benefits of using threads for concurrency
is the potential to easily and directly share data between
them.

However there are some issues surrounding shared data


A race condition is an undesirable situation that occurs when a device or
system attempts to perform two or more operations at the same time, but
because of the nature of the device or system, the operations must be done
in the proper sequence to be done correctly.
• In computing, the producer-consumer problem is a family of
problems described by Edsger W. Dijkstra since 1965.
• Rainer Grimm, Concurrency with Modern C++, March (LeanPub) 2019.
• https://en.cppreference.com/w/cpp

You might also like