So many threading primitives have different names on different platforms. This is basically Go's WaitGroup, except the C++ latch can't be incremented (it's otherwise identical). I wonder why that is?
/// ----------------------------------------------------------------------------
/// latches are great for multi-threaded tests with following sequence of steps
/// 1. setup test data
/// 2. create a latch
/// 3. create test threads each decrementing the latch-count
/// 4. when all threads have reached the latch, they are unblocked.
///
/// sligtly verbose commented code to illustrate this
void foo()
{
/// --------------------------------------------------------------------
/// create a latch with non-zero-count
unsigned const thread_count=...;
std::latch done(thread_count);
my_data data[thread_count];
std::vector<std::jthread> threads;
/// --------------------------------------------------------------------
/// threads decrement count as they are created
for(unsigned i=0;i<thread_count;++i)
threads.push_back(std::jthread([&,i]{
data[i]=make_data(i);
done.count_down();
do_more_stuff();
}));
/// --------------------------------------------------------------------
/// others wait for latch to be signalled, when the count reaches zero,
/// latch is permanently signalled, and threads are woken
done.wait();
process_data();
}
have fun ? var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
}()
wg.Wait()
The middle part of the block can be done freely, you don't have to count the threads beforehand. ...
wg.Add(1)
...
for every thread that you are about to create ?The phrasing is somewhat confusing: it is the waiting thread, not "they", that gets unblocked (count_down does not block).
EitherOwnedOrBorrowed and AtomicReferenceCountedPointer, but that'd be annoying to type once you know them.
code is read more than it is written, so having those longer terms helps with reducing mental load and providing self-documenting codeand with most modern ide or even text editors providing autocompletion, (imho) i think abbreviations not really neccessary (though they can be fun in the case of kotlin ^^)
I too would love to know the specific reasoning though.
The C++ stdlib terminology is always so weird.
I still miss usint Ctrl-P when working with VS Code.