6 ms·
I'm a little disappointed in the name because latch already has a specific meaning for computer engineers. It is one of the first memory circuits we study and f
by goalieca 6y ago
I'm a little disappointed in the name because latch already has a specific meaning for computer engineers. It is one of the first memory circuits we study and forms the basis for registers.
- dataflow 6y agoYeah this confused me too. Not sure what made them think this resembles a latch. A latch only has 2 states, not 2^n states.
- vips7L 6y agoThe full name is typically CountDownLatch [0]. The latch still has 2 states, closed and open. It's just that it doesn't open until everyone is ready. [0] https://docs.oracle.com/en/java/javase/15/docs/api/java.base/java/util/concurrent/CountDownLatch.html https://docs.oracle.com/en/java/javase/15/docs/api/java.base...
- dataflow 6y agoThat's like saying every integer has 2 states, zero and nonzero. I'm talking states in the computer science sense, not in the "what I prefer to look at" sense. These have 2^n states, whether you choose to care about them or not. Just because Java calls it something that doesn't mean it is "typically" called that thing.
- vips7L 6y agoRepresenting it as a "state machine" it would still have only 2 states. Open and closed. I personally "prefer to look at" integers as not having state. > Just because Java calls it something that doesn't mean it is "typically" called that thing. It's only been there since 2004 and is named the same in C#
- dataflow 6y ago> Representing it as a "state machine" it would still have only 2 states. Open and closed. No. There are lots of "open" states with different counter values. That you choose to call them all "open" and not observe the differences doesn't change this. It sounds like you've never studied state machines, so I recommend reading up on them; here's a starting point: https://en.wikipedia.org/wiki/State_(computer_science)#Finite_state_machines https://en.wikipedia.org/wiki/State_(computer_science)#Finit... > It's only been there since 2004 and is named the same in C# No it's not... where did you get this? C# has CountdownEvent and Barrier. It's not called "latch". And, for that matter, OpenMP also has #pragma omp barrier since 1998... https://www.openmp.org/wp-content/uploads/cspec10.pdf#page=26 https://www.openmp.org/wp-content/uploads/cspec10.pdf#page=2... There's no point continuing this argument so let's just leave it here.
- vips7L 6y ago> There's no point continuing this argument so let's just leave it here. Yeah cause you're insufferable.
- dang 6y agoOuch, please don't cross into personal attack, regardless of how annoying another comment is or you feel it is! We ban accounts that do that. Fortunately your comment history looks like a good one otherwise. If you wouldn't mind reviewing https://news.ycombinator.com/newsguidelines.html https://news.ycombinator.com/newsguidelines.html and sticking to the rules when posting here, we'd be grateful.
- dang 6y agoPlease avoid swipes in your comments like "It sounds like you've never studied state machines", "where did you get this?" and "There's no point continuing this argument so let's just leave it here". They invariably land with much more force on the other person than you intend, and this leads to much worse threads. It's easy to understand see how this post would turn into a provocation. https://news.ycombinator.com/newsguidelines.html https://news.ycombinator.com/newsguidelines.html
- zvrba 6y agoIt makes sense when compared against flip-flop circuit https://www.geeksforgeeks.org/difference-between-flip-flop-and-latch/ https://www.geeksforgeeks.org/difference-between-flip-flop-a... Most importantly, latch is level-triggered, just like the synchronization primitive.
- dataflow 6y ago> It makes sense when compared against flip-flop circuit It doesn't to me... I'm not following unfortunately. Barriers are level-triggered too. Like, physical ones. Push with enough force against a barrier and it'll break down.
- kazinator 6y agolatches can be edge or level triggered. The important concept is that once they are triggered, they latch the input value, and maintain it after the input has disappeared.
- kllrnohj 6y agoThat's what std::latch does... Once triggered it's always triggered. Same as Java's CountDownLatch
- kazinator 6y agoThe thing is, pretty much every object you work with in your programming language latches the most recently stored value in it. The machine is built on latching as a fundamental block. A simple mutex latches open when you unlock it, and then latches locked when something locks it again.
- kllrnohj 6y agoNo, I mean, a std::latch cannot be reset. It's a one-direction one-use toggle. There's no unlocking or relocking. There's no "again" to it.
- kllrnohj 6y agoNote the name isn't new for this concept, Java also uses the latch name to describe this: https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/CountDownLatch.html https://docs.oracle.com/javase/7/docs/api/java/util/concurre...
- ppg677 6y agoApparently databases historically call shared-memory locks "latches" to distinguish them from locks used for transaction conflict detection.
- pfarrell 6y agoHad a mentor who said one of the biggest problems in computer science is that all the good metaphors are already taken.
- dataflow 6y agoBut in this case the concept already had a name in CS and it was called a barrier (which is a pretty apt name too). For some rename they called it a latch and then they called a cyclic barrier a barrier...
- gpderetta 6y agoAs far as I can tell, a (countdown) latch is different from a barrier. In a barrier signaling and waiting is a single atomic operation. Each thread (of a group of N) reaching a barrier will wait until all N threads have reached an waited (and implicitly signaled) it. A countdown latch is an event that will release one (or more) waiter only after has been signaled N times. Signaling and waiting threads are not necessarily the same and often are distinct sets. I guess you could build a barrier from a countdown latch, but I suspect that a trivial mapping of barrier::wait to latch::signal+wait is going to racey and you need an additional sinchronization primitive. For example pthread_barrier_wait requires an additional mutex (similar to condition variables). edit: std::barrier has (optionally) separate singal+wait and uses an explicit arrival token instead of a mutex to tie the signaling with waiting. In practice a barrier and a countdown latch are used for different purposes (the former to coordinate multiple symmetric threads across phases of a distributed computation, the latter to wait for completion of N events).
- dataflow 6y ago> In a barrier signaling and waiting is a single atomic operation. Each thread (of a group of N) reaching a barrier will wait until all N threads have reached an waited (and implicitly signaled) it. Yeah that's exactly what std::latch::arrive_and_wait does. I don't think atomicity is a thing here though... arrive_and_wait() is just count_down() followed by wait(). > A countdown latch is an event that will release one (or more) waiter only after has been signaled N times. Signaling and waiting threads are not necessarily the same and often are distinct sets. C# still calls this a Barrier, except that it requires one of the waiters to be a participant (see AddParticipant() and SignalAndWait()): https://docs.microsoft.com/en-us/dotnet/api/system.threading.barrier?view=net-5.0 https://docs.microsoft.com/en-us/dotnet/api/system.threading... They even allow removing participants! This is a little bit like a mutex that has a try_lock. It's not strictly the vanilla Computer Science "mutex" with lock/unlock per se, but it's not a fundamentally different concept; it's just a handy yet pretty close generalization. I guess if you really want to give this a new name then maybe it's not a terrible idea given it's a slight generalization of a barrier, but latch is certainly not going to be any more accurate (or less confusing) than barrier.
- wyldfire 6y agoAnd barrier instructions/semantics are used by many architectures to flush/partition pipelines. AFAICT this one doesn't do that. Too bad they couldn't qualify it somewhere further down below std::. Something like std::thread::barrier or similar might be easier to understand.
- kazinator 6y agoHowever, the barrier metaphor is correct. The counter reaches a certain level (the barrier potential, get it?) upon which the object triggers.
- hn_go_brrrrr 6y agoUnfortunately, nested namespaces can have some surprising and unintended consequences. You might find https://abseil.io/tips/130 https://abseil.io/tips/130 interesting. Given the author was until very recently the chair of the C++ LEWG, I imagine he's had some influence on how what namespaces new types ended up in.
- thamer 6y agoNot 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.
- ur-whale 6y agoIt will also be annoying for HW designers ... latches and flip-flops have a very specific meaning in their world.
- tlamponi 6y agoYou repeat OP 1:1. Computer engineers are HW designers, may not go into that field but it's pretty engrained in their courses. source: am computer engineer.
- magicalhippo 6y agoThe name makes a bit of sense, in the sense that once it's signaled, it stays that way. So the signaled state is latched. This contrasts to std::barrier which auto-resets once signaled[1]. As such I think it would be better if they had called it std::latched_barrier or similar. [1]: https://en.cppreference.com/w/cpp/thread/barrier https://en.cppreference.com/w/cpp/thread/barrier
- l8rlump 6y agoThe name has been overloaded ;)