Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/msaf1980/cyclicbarrier
https://github.com/msaf1980/cyclicbarrier
cyclic-barrier go golang sync synchronization
Last synced: about 1 month ago
JSON representation
- Host: GitHub
- URL: https://github.com/msaf1980/cyclicbarrier
- Owner: msaf1980
- License: mit
- Created: 2019-04-29T06:55:39.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2019-04-29T17:36:38.000Z (over 5 years ago)
- Last Synced: 2024-04-30T10:28:15.088Z (8 months ago)
- Topics: cyclic-barrier, go, golang, sync, synchronization
- Language: Go
- Size: 4.88 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# cyclicbarrier
CyclicBarrier is a synchronizer that allows a set of goroutines to wait for each other to reach a common execution point, also called a barrier.
### Usage
Initiate
```go
import "github.com/msaf1980/cyclicbarrier"
...
b1 := cyclicbarrier.New(10) // new cyclic barrier with parties = 10
```
Await
```go
b.Await() // await other parties
```
Break Barrier
```go
b.BreakBarrier() // break the barrier
```
Break Barrier
```go
broken := b.IsBroken() // break barrier status
```### Simple example
```go
// create a barrier for 10 parties with an action that increments counter
b := cyclicbarrier.New(10)wg := sync.WaitGroup{}
for i := 0; i < 10; i++ { // create 10 goroutines (the same count as barrier parties)
wg.Add(1)
go func() {
defer wg.Donefor j := 0; j < 5; j++ {
// do some hard work 5 times
time.Sleep(100 * time.Millisecond)b.Await() // ..and wait for other parties on the barrier.
// Last arrived goroutine will do the barrier action
// and then pass all other goroutines to the next round
}
}()
}wg.Wait()
```