C++20 Concurrency-4: Latches
1. Introduction
C++20 extended the concurrency features to enable concurrent threads to block at a certain point in the execution with the help new synchronization primitives : std::latch
and std::barrier
and semaphores . Synchronization primitives have long existed as a part of concurrency features C++ for e.g. std::mutex
, std::condition_variables
, std::promise
and std::future
. The goal of this article is to explain std::latch
.
An analogy with real life example is latches on door that act as barrier preventing unwanted agents (humans, pets, etc ) to pass from one place to another.
In simple terms, std::latch
in C++20 is a synchronization construct that blocks multiple threads at the point of latch. Typically such mechanisms are employed to ensure all threads have finished same or different tasks (function execution) before they can proceed further. In other parallel programming API’s like OpenMP, MPI, these constructs exist in one form or another in the form of barrier synchronization.
In contrast std::mutex
, which stands for mutual exclusion, allows only a single thread to execute a given block of code and acts as a barrier for the…