A countdown latch (a.k.a. CountDownLatch) is a synchronization primitive that ensures that all of the used resources are released before the primitive is destroyed. It is like a QSemaphore
, but working in reverse: we want to block not to acquire a resource, but to ensure that all of them have been released.
What would be an easy way to implement it in Qt?
Here's an implementation that leverages
QSemaphore
:To use, keep an instance of
CountdownLatch::Locker
while you want the latch to stay blocked. The latch's destructor will block until all locker's are destroyed.The instance
a
will stay around until the concurrent worker is done, i.e. for 10 seconds.