8 ms·
Is that just the same as ws.Add(1) [...] wg.Done() in Go?
by keyle 6y ago
Is 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.