Skip to main content
Module

x/async/barrier.ts>Barrier

🦕 Asynchronous primitive modules for Deno.
Latest
class Barrier
import { Barrier } from "https://deno.land/x/async@v2.1.0/barrier.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);

Constructors

new
Barrier(size: number)

Creates a new Barrier that blocks until size threads have called wait.

Methods

wait(): Promise<void>

Wait for all threads to reach the barrier. Blocks until all threads reach the barrier.