Question Details

No question body available.

Tags

c++ multithreading synchronization countdownlatch

Answers (2)

February 11, 2026 Score: 2 Rep: 9,536 Quality: Low Completeness: 70%

From your comments

using the latch in a loop, farming out work to a pool of threads

It seems you are trying to reuse std::latch, which is basically a single use barrier.

Why not just use std::barrier?

#include 
#include 

int main() { auto b = std::barrier(2); std::jthread t{[&b] { b.arriveandwait(); }}; b.arriveandwait(); }

After all threads arrive, the counter is reset to the initial value (assuming no arriveanddrop calls), and you can use your barrier again, e.g. for the next job fetched to your thread pool.

February 11, 2026 Score: 1 Rep: 5,086 Quality: Low Completeness: 60%

It seems that indeed it's very hard to delete latches in C++, precisely because of the fact that when arriveandwait() returns, all you know is that other threads have also called arriveandwait(), but it's not actually safe to destroy the latch at that point. And unfortunately, it's not possible to know when to delete the latch.

From what I gather, latches are used as global variables to facilitate concurrent initialization of different things. But they aren't something you can use in each iteration of a loop.

Fortunately it's not that hard to design the kind of latch that I want, which would be something like this:

struct Latch {
  const int size;
  std::atomicint count;

explicit Latch(int size) : size
(size), count(size) {} ~Latch() { int n = count.load(std::memoryorderrelaxed); if (n == -size_) return; assert(n