11 ms·
Latches in C++20
- p2t2p 6y agoAnd that's why I love Java. Somehow it's been there for almost 20 years.
- ncmncm 6y agoWhy I like Java, by Mark Dominus https://blog.plover.com/prog/Java.html https://blog.plover.com/prog/Java.html "I enjoyed programming in Java, and being relieved of the responsibility for producing a quality product."
- vips7L 6y ago(2014) Yeah java 6/7 was verbose, but it's nothing representative of the language now. You might as well point out the problems of C++03. Streams, lambdas, method references, records, and local variable inference make it pretty fluent to write and at the very least it has a sane standard library (I can't say as much for Scala). Also as for their intro problem what type of Java programmer worth their salt can't do: while (true) System.out.print(System.in.read());
- p2t2p 6y agoThis is something I always read from people who produce a puddle of noodle soup that keeps crunching through corner cases instead of producing something structured and actually readable by human beings.
- PaulDavisThe1st 6y agoGlib::Cond has existed for longer, and is just a wrapper around pthread condition variables that go back to the 1980s. Several other C++ libraries also provided condition variables, which latches are a variant of, along with barriers and flex_latches.
- Thorrez 6y agostd::condition_variable existed since c++11, so std::latch must provide some benefit beyond it.
- DSingularity 6y agoPerhaps differentiation is justified as program semantics can be captured to allow optimizations in runtimes. For example, condition variable to wait for events which can be delayed vs latched to wait for arrival of running threads to a point of execution. Runtime should handle each uniquely.
- PaulDavisThe1st 6y agoThe semantics of latch are a little simpler, which leaves room for a lighter weight implementation than is typically associated with condition variables. Barriers are closer, but also have slightly simpler semantics, again allowing for a potentially lighter weight implementation. But the semantics are not sufficiently different as to allow for any notably new use cases/code flow, certainly not from the examples I've seen. Every one of them could have been written using condition variables at any point in the last 30 years and would look almost indistinguishable.
- jillesvangurp 6y agoWorth noting Doug Lee's early work on adding concurrency to Java. That effort resulted in the java.util.concurrency package but work started in the late nineties and it was available as a separate library. And of course it included a Latch class; and the related CountDownLatch. http://gee.cs.oswego.edu/dl/classes/EDU/oswego/cs/dl/util/concurrent/intro.html http://gee.cs.oswego.edu/dl/classes/EDU/oswego/cs/dl/util/co... Before this stuff landed, Java's concurrency model was a lot less nice to deal with. Good high level abstractions are important. Nice to see the same kind of primitives are being added to C++.
- goalieca 6y agoI'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.
- The_rationalist 6y agoDoes the Java ecosystem offer something similar?
- ridiculous_fish 6y agoHow are latches implemented? Is it more efficient than just a condition variable?
- amluto 6y agoAlmost certainly. Condition variables are very complicated internally. On Linux, a latch could be backed by a futex with very little code.
- ridiculous_fish 6y agoWhat is the source of the complexity inside condition variables? Reading the glibc sources, the main problem seems to be ensuring that signals are delivered properly?
- zvrba 6y agoYes, atomic unlock + wait is the complex bit. Prior to Vista, Windows did not have a CV primitive. Existing emulations used multiple kernel objects and their correctness was difficult to verify. Performance also suffered.
- gpderetta 6y agoI think Vista implemented keyed synchronization objects (i.e. a flavour of futexes) which have similar edge triggered semantics as CVs.
- atq2119 6y agoIt's possible to implement very simple condition variables. The tricky part is preventing correct but inefficient behaviours like stampeding herds.
- gpderetta 6y agoWhen implementing posix semantics there are a lot of nasty corner cases, especially with dealing with fairnes, graceful destruction, correct handling of wait generations and probably more that I'm missing. I would never attempt to implement a full condvar.
- keyle 6y agoIs that just the same as ws.Add(1) [...] wg.Done() in Go?
- nkozyra 6y agoSmall typo above but it seems like a different approach to mutex. What's similar is basically a count of operations that's locked between threads/routines.
- keyle 6y agoHaha yes sorry, a Freudian slip of me working with web sockets at the moment.
- Thorrez 6y agoIn most cases I think they're identical. WaitGroup has an ability that latch doesn't: WaitGroup can Add() and subtract (Add() with negative) whereas latch can only subtract (latch has to be initialized with its maximum value). The fact that latch is initialized with its value could be a little more convenient than WaitGroup in some situations (save 1 line of code). latch has an ability that WaitGroup doesn't: .try_wait(). Also latch's .arrive_and_wait() can save a line of code compared to WaitGroup.
- xyproto 6y agoSaving lines of code in C++ compared to Go is like fighting windmills.
- pferde 6y agoNot quite. It is not about reducing the program line count, but rather about reducing cognitive load for writing correct code. If you have to specify the initial count, then you cannot introduce an error where setting the initial count is forgotten.
- nkozyra 6y agoGo's approach seems a little more prone to user error. I know I've been tripped up by not explicitly adding a worker.
- deleted 6y ago[deleted]
- dataflow 6y agoIt sounds like "latch" is basically the same thing as an "event" (as in CreateEvent() in Windows, eventfd in Linux), but maybe optimized for single-process usage? Edit: Ok so a (Windows) event is one bit, a C++ latch is a one-shot counter, and a C++ barrier is a cyclic counter. But I thought a barrier in general computer science terms is just a one-shot counter, which is what they're calling a latch in C++?
- deleted 6y ago[deleted]
- qwertywert_ 6y ago"When you think about this workflow, you may notice that it can be performed without a boss." hehe
- Ice_cream_suit 6y agoVery poorly and confusingly named.
- liquidify 6y agoI love multithreading. So much fun.
- varajelle 6y agoWhat's the difference with a semaphore?
- giu 6y agoA semaphore can be used to control thread access to a resource. A latch allows one or more threads to wait until a set of operations being performed in other threads completes [0]. [0] https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/CountDownLatch.html https://docs.oracle.com/javase/8/docs/api/java/util/concurre...
- magicalhippo 6y agoA semaphore allows threads to pass until the counter reaches zero. A barrier (what this thing is) blocks threads until the counter reaches zero.
- eptcyka 6y agoWhy use the bank account example to introduce a problem and then never resolve it with latches?
- ibobev 6y agoDoes someone have any idea why latches do not have also count up operation alongside count down? In one of my previous jobs, I implemented a latch with a mutex, condition variable, and a counter and I supported also count up. It seems to work OK and count up was an essential operation for the use case I had needed it. class ThreadLatch { public: ThreadLatch(std::size_t count = 0) : _count(count) {} void inc() { std::lock_guard<std::mutex> lock(_mutex); ++_count; } void dec() { std::lock_guard<std::mutex> lock(_mutex); assert(0 != _count); --_count; if(0 == _count) { _condition.notify_all(); } } void wait() { std::unique_lock<std::mutex> lock(_mutex); while(_count > 0) { _condition.wait(lock); } } private: std::mutex _mutex; std::condition_variable _condition; std::size_t _count; };