Skip to content
master
Switch branches/tags
Code

Latest commit

 

Git stats

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
 
 
 
 
 
 
 
 
 
 

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 := semaphore.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.

About

A very basic semaphore using channels.

Resources

License

Releases

No releases published

Packages

No packages published

Languages