import { Barrier } from "https://deno.land/x/async@v2.1.0/mod.ts";
A synchronization primitive that allows multiple tasks to wait until all of them have reached a certain point of execution before continuing.
A Barrier
is initialized with a size n
. Once created, n
tasks can call
the wait
method on the Barrier
. The wait
method blocks until n
tasks
have called it. Once all n
tasks have called wait
, all tasks will
unblock and continue executing.
import { Barrier } from "https://deno.land/x/async@v2.1.0/barrier.ts";
const barrier = new Barrier(3);
async function worker(id: number) {
console.log(`worker ${id} is waiting`);
await barrier.wait();
console.log(`worker ${id} is done`);
}
worker(1);
worker(2);
worker(3);