Skip to content
A very basic semaphore using channels.
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.gitignore
.travis.yml
LICENSE
README.md
semaphore.go
semaphore_test.go

README.md

semaphore

A very simple semaphore using channels.

Go Report Card Build Status

Installation

go get -u -d github.com/codingconcepts/semaphore

Usage

The following example kicks off 1,000,000 goroutines, each of which increment a sum variable atomically. After all operations have been kicked off, we ask the semaphore to wait for them to complete.

sum := int64(0)

sem := New(10)
for i := 0; i < 1000000; i++ {
	sem.Run(func() {
		atomic.AddInt64(&sum, 1)
	})
}
sem.Wait()

fmt.Println(sum)
// Prints: 1,000,000

Note that if you call Wait() before all operations have been kicked off (and don't call it again at the end), the result of sum is not guaranteed.

You can’t perform that action at this time.