Skip to content

Implementation of POSIX-style semaphores in Go

License

Notifications You must be signed in to change notification settings

UoB-CSA/semaphore

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

🚦 Semaphore

Semaphore provides a simple POSIX-style implementation of semaphores. Internally it uses buffered channels, but the exposed methods should be familiar to all C programmers.

Install

You can install this library using go get -u github.com/ChrisGora/semaphore.

Basic Usage

Creating a new semaphore

semaphore.Init(max, value) takes a maximum value and initial value for the new semaphore.

maxValue := 3
initValue := 0

sem := semaphore.Init(maxValue, initValue)

Wait / Pend / P

To decrease the value of the semaphore (also known as waiting, pending, etc.):

sem.Wait()

Post / Signal / V

To increase the value of the semaphore (also known as posting, signalling, etc.):

sem.Post()

Example Usage

Producer-Consumer

The producer-consumer problem can be solved with the semaphores and Go's mutexes in the following way:

func producer(buffer *buffer, spaceAvailable, workAvailable semaphore.Semaphore, mutex *sync.Mutex) {
	for {
		spaceAvailable.Wait()
		mutex.Lock()
		buffer.put(1)
		mutex.Unlock()
		workAvailable.Post()
	}
}

func consumer(buffer *buffer, spaceAvailable, workAvailable semaphore.Semaphore, mutex *sync.Mutex) {
	for {
		workAvailable.Wait()
		mutex.Lock()
		_ = buffer.get()
		mutex.Unlock()
		spaceAvailable.Post()
	}
}

About

Implementation of POSIX-style semaphores in Go

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Go 100.0%