CS0051 - Module 02
CS0051 - Module 02
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>
int main() {
}
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
HelloObject ho;
thread t2(&HelloObject::objectfunction, &ho); //runs HelloObject::objectfunction() on object
ho
t1.join()
t2.join()
}
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();
}
#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); }
};
class HelloObject {
public:
void objectfunction(int n, string str);
};
std::chrono::duration - cppreference.com
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.
Code Demo of Producer-Consumer Problem
(Race Condition)
• Mutex stands for mutual exclusion. It ensures that only one thread can
access a critical section at any one time.
Key Points:
• Atomic Operations: These are operations that complete without
interruption, preventing race conditions.
• Thread Safety: std::atomic makes sure that multiple threads can safely
read and write shared data.
• Efficiency: Many atomic operations are lock-free, meaning they don't use
mutexes and are faster.
#include <atomic> int main() {
#include <iostream> std::thread t1(increment);
#include <thread> std::thread t2(increment);
void check_b_then_a() {
if (b.load() == 1 && a.load() == 0) {
std::cout << "Race condition detected!" << std::endl;
}
}
Using std::lock_guard in C++ is an elegant way to manage mutexes and ensure that they
are properly locked and unlocked, preventing potential deadlocks and race conditions.
Here's a simple guide to using std::lock_guard:
#include <iostream>
int main() {
#include <thread> thread t1(print_message, "Hello from thread 1");
#include <mutex> thread t2(print_message, "Hello from thread 2");
When the guard object goes out of scope (e.g., the function exits), the mutex is
automatically unlocked.
This ensures that the critical section (accessing std::cout in this case) is safely
managed.
Code Demo of Producer-Consumer Problem
(Using lock guards)
• Rainer Grimm, Concurrency with Modern C++, March (LeanPub) 2019.
• https://en.cppreference.com/w/cpp