Count Down Latch
A one-shot concurrency primitive that blocks any fibers that wait on
it. It is initialized with a positive integer n
latches and waiting
fibers are semantically blocked until all n
latches are released.
After this, further await
s are no-ops (equivalent to IO.unit
).
trait CountDownLatch[F[_]] {
def release: F[Unit]
def await: F[Unit]
}
Cancelation of await
is supported.
import cats.implicits._
import cats.effect._
import cats.effect.std.CountDownLatch
import cats.effect.unsafe.implicits.global
val run = (
for {
c <- CountDownLatch[IO](2)
f <- (c.await >> IO.println("Countdown latch unblocked")).start
_ <- c.release
_ <- IO.println("Before latch is unblocked")
_ <- c.release
_ <- f.join
} yield ()
)
// run: IO[Unit] = FlatMap(
// ioe = FlatMap(
// ioe = Map(
// ioe = Delay(
// thunk = cats.effect.IO$$$Lambda$17644/0x0000000803d2e440@33078845,
// event = cats.effect.tracing.TracingEvent$StackTrace
// ),
// f = cats.effect.std.CountDownLatch$State$$$Lambda$17645/0x0000000803d2c840@25f0ccef,
// event = cats.effect.tracing.TracingEvent$StackTrace
// ),
// f = cats.effect.std.CountDownLatch$$$Lambda$17646/0x0000000803d2c040@c362740,
// event = cats.effect.tracing.TracingEvent$StackTrace
// ),
// f = <function1>,
// event = cats.effect.tracing.TracingEvent$StackTrace
// )
run.unsafeRunSync()