semaphore

package module
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 14, 2019 License: MIT Imports: 0 Imported by: 0

README

🚦 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.

Example usage

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()
	}
}

Documentation

Overview

Package semaphore provides a POSIX-style semaphore implementation.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Semaphore

type Semaphore interface {
	Post()
	Wait()
	GetValue() int
}

func Init

func Init(max, value int) Semaphore

Init creates a new semaphore with an initial value and some upper bound. This is the only difference compared to POSIX semaphores.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL