Multithreaded programming is inherently difficult. One of the reasons is that we can't have control over how a thread will start and finish - in which order - it all depends upon the thread scheduling algorithm of the OS. This makes the reproduction of test cases difficult. Moreover, there are race conditions and deadlocks.
When I was teaching the Countdown latch - a thread synchronization technique used in the Java Concurrency package, there was none like that available in C++. I am happy to see that the concept of latch is introduced in C++20.
So...
What is a Latch in C++?
- A synchronization primitive was introduced in C++20.
- It allows one or more threads to wait for a certain number of operations to complete before proceeding.
- Acts like a countdown counter that blocks threads until it reaches zero.
/*
* Student.h
*
* Created on: Jan 7, 2024
* Author: som
*/
#ifndef STUDENT_H_
#define STUDENT_H_
#include <iostream>
#include <string>
#include <thread>
#include <latch>
using namespace std;
class Student {
private:
string name;
int timeToFinish;
public:
Student(string name, int timeToFinish){
this->name = name;
this->timeToFinish = timeToFinish;
}
virtual ~Student(){
}
void task(std::latch& l){
cout<< this->name << " is Starting the task"<<endl;
this_thread::sleep_for(chrono::milliseconds(this->timeToFinish));
cout<< this->name <<" finished the task"<<endl;
l.count_down();
}
};
#endif /* STUDENT_H_ */
/*
* classETC.h
*
* Created on: Jan 7, 2024
* Author: som
*/
#ifndef CLASSETC_H_
#define CLASSETC_H_
#include <thread>
#include <latch>
#include "Student.h"
class classETC {
public:
classETC(){
}
virtual ~classETC(){
}
void giveTaskToStudent(latch& l){
thread riditT(&Student::task, new Student("Ridit", 1000),ref(l));
thread ishanT(&Student::task, new Student("Ishan", 3000), ref(l));
thread rajdeepT(&Student::task, new Student("Rajdeep", 900), ref(l));
cout<<"Teacher is waiting for all the students to finish their task"<<endl;
riditT.join();
ishanT.join();
rajdeepT.join();
l.wait();
cout<<"All students submitted their task... Teacher is leaving the class"<<endl;
}
};
#endif /* CLASSETC_H_ */
Main:
#include <iostream>
#include <latch>
#include <thread>
#include <chrono>
#include "classETC.h"
using namespace std;
int main() {
cout << "C++ Version" << endl; // prints C++ Version
if (__cplusplus == 202101L)
cout << "C++23";
else if(__cplusplus == 202002L)
cout << "C++20\n";
else if (__cplusplus == 201703L)
cout << "C++17\n";
else if (__cplusplus == 201402L)
cout << "C++14\n";
else if (__cplusplus == 201103L)
cout << "C++11\n";
else if (__cplusplus == 199711L)
cout << "C++98\n";
else cout << "pre-standard C++\n";
latch l(3);
classETC etc;
etc.giveTaskToStudent(l);
return 0;
}
Explanation of the code:
For your reference, here's my son Ridit on Java CountdownLatch.
#Enjoy
No comments:
Post a Comment