threading/barrier

Search:
Group by:
Source   Edit  

Barrier for Nim.

Example:

import threading/barrier

import std / os

var phases: array[10, int]
var b = createBarrier(10)

proc worker(id: int) =
  for i in 0 ..< 100:
    phases[id] = i
    if (id + i) mod 10 == 0:
      sleep 1
    wait b
    for j in 0 ..< 10:
      assert phases[j] == i
    wait b

var threads: array[10, Thread[int]]
for i in 0..<10:
  createThread(threads[i], worker, i)

joinThreads(threads)

Types

Barrier = object

A barrier is a synchronization mechanism that allows a set of threads to all wait for each other to reach a common point. Barriers are useful in programs involving a fixed-size party of cooperating threads that must occasionally wait for each other. The barrier is called a cyclic barrier if it can be reused after the waiting threads are released.

The barrier is initialized for a given number of threads. Each thread that calls wait on the barrier will block until all the threads have made that call. At this point, the barrier is reset to its initial state and all threads are released.

Source   Edit  

Procs

proc `=copy`(dest: var Barrier; src: Barrier) {.error.}
Source   Edit  
proc `=destroy`(b: Barrier) {.inline, ...raises: [], tags: [], forbids: [].}
Source   Edit  
proc `=sink`(dest: var Barrier; src: Barrier) {.error.}
Source   Edit  
proc createBarrier(parties: Natural): Barrier {....raises: [], tags: [],
    forbids: [].}
Source   Edit  
proc wait(b: var Barrier) {....raises: [], tags: [], forbids: [].}
Wait for all threads to reach the barrier. When the last thread reaches the barrier, all threads are released. Source   Edit