CyclicBarrier

class CyclicBarrier(val capacity: Int, barrierAction: () -> Unit = {})(source)

A CyclicBarrier is a synchronization mechanism that allows a set of coroutines to wait for each other to reach a certain point before continuing execution. It is called a "cyclic" barrier because it can be reused after all coroutines have reached the barrier and released.

To use a CyclicBarrier, each coroutine must call the await method on the barrier object, which will cause the coroutine to suspend until the required number of coroutines have reached the barrier. Once all coroutines have reached the barrier they will resume execution.

Models the behavior of java.util.concurrent.CyclicBarrier in Kotlin with suspend.

Parameters

capacity

The number of coroutines that must await until the barrier cycles and all are released.

barrierAction

An optional runnable that will be executed when the barrier is cycled, but before releasing.

Constructors

Link copied to clipboard
constructor(capacity: Int, barrierAction: () -> Unit = {})

Properties

Link copied to clipboard

Functions

Link copied to clipboard
suspend fun await()

When await is called the function will suspend until the required number of coroutines have called await. Once the capacity of the barrier has been reached, the coroutine will be released and continue execution.

Link copied to clipboard
suspend fun reset()

When called, all waiting coroutines will be cancelled with CancellationException. When all coroutines have been cancelled the barrier will cycle.