6 ms·
Not meaning to disregard your point, but the class doing the same thing as `std::latch` in the JDK is called `CountDownLatch`[1] and is used in a similar way:
by thamer 6y ago
Not meaning to disregard your point, but the class doing the same thing as `std::latch` in the JDK is called `CountDownLatch`[1] and is used in a similar way:
CountDownLatch workDone = new CountDownLatch(6);
workDone.countDown(); // mark an event
workDone.await(); // wait until the counter reaches zero
Even if latches also refer to another concept, it's still positive to see some naming consistency across languages. What name would you have preferred?
[1] https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/concurrent/CountDownLatch.html https://docs.oracle.com/en/java/javase/11/docs/api/java.base...
- kazinator 6y agoThat's a barrier. A latch is a circuit that takes on a value (high or low, 1 or 0) when some some gating signal arrives (like a clock pulse) and then holds that value after the input is removed. It is named after a door latch; a door latches when you close it and then holds that state: you can't pull it open again without using the handle/knob. If something is called "latch" which does not change its state once in order to reflect an input event, and then hold that state until explicitly recent, then it's misnamed due to abusing the metaphor. The barrier metaphor is the right one for an object that is hit some predetermined number of times and then fires an event (like releasing some waiting thread(s)). That predetermined number of events is its "barrier potential": the threshold that must be met to break through the barrier.
- thamer 6y agoI see what you mean, but since the topic is concurrency "barrier" is also a term used for a different concept in this domain: memory barriers. N=1 of course but if I was asked what a barrier was in the context of concurrency I would immediately think of memory barriers, not the system we're talking about here.
- kazinator 6y agoPOSIX has had barriers since around 2000 or so: pthread_barrier_wait and friends. Memory barriers and POSIX-style barriers are semantically related; both mechanisms ensure that certain events are not re-ordered between adjacent phases; they stay on their side of the barrier.
- gpderetta 6y agoYes; the C++ multithreading support owes a lot (among many influences) to pthreads and one reason that low level memory barriers were called fences was to reserve the barrier name to a possible pthread-like construct.
- kazinator 6y agoMemory barriers also being called fences ("fence instruction") long precedes C++ multithreading. (That conflicts with "fence register"; a concept in non-virtualized memory management whereby each running process is confined to accessing range of memory delimited by values held in fence registers.)
- kitd 6y agoIt seems to me the only difference is the number of input values. N inputs latches open (N can be 1).
- kllrnohj 6y agoThis doesn't change state after being signaled, hence why it's called a latch. The reusable variant is called a std::barrier.
- xmprt 6y agoI have limited knowledge of the Java and C++ libraries but I believe Go has something similar called a WaitGroup.