DEV Community

Somenath Mukhopadhyay
Somenath Mukhopadhyay

Posted on

Latches in C++ 20 concurrency - just like the CountdownLatch of Java concurrency package...

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.

Good to know that the C++ team is trying to catch up with Java...

Here we go...

My experimentation with C++ latches.

I taught my young son Ridit about the Java Countdown latch three years ago.

Good to see the C++ team is making the standard library more powerful day by day.

Here is the C++ source code of my experimentation.

The following C++ code needs C++20 to compile and execute.

/*

* 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_ */
Enter fullscreen mode Exit fullscreen mode
/*

* 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_ */

Enter fullscreen mode Exit fullscreen mode

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;
}
Enter fullscreen mode Exit fullscreen mode

Explanation of the code:

Have a look at the Student class. Each student will do his work in a

background thread. As there are 3 students, the latch will be initiated with 3.

After completing the task, a student will reduce the latch counter by 1.

So when all the students will complete the task, the latch - on which

the main thread was blocked, will become zero and it will unblock the main thread.
The C++ latches work exactly the way the Java Countdown Latch
works.

Top comments (0)