Skip to content

codingconcepts/semaphore

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages