Help us shape the future of Go! Take the 2026 Go Developer Survey

sema

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Feb 14, 2019 License: Apache-2.0 Imports: 2 Imported by: 1

README

Sema Build Status Go Report Card

Sema is a simple semaphore implementation for Go (golang) using channels to control concurrent access to shared resources. (Go still lacks a user accessible semaphore implementation in the standard library)

Example

Install
go get github.com/brunotm/sema
Usage

package main
import (
	"time"
	"github.com/brunotm/sema"
)

func main() {
	max := 100
	s := sema.New(max)

	s.Acquire()
	defer s.Release()
	// DO WORK...

	// OR
	if s.TryAcquire() {
		defer s.Release()
		// DO WORK...
	}

	// OR
	if s.AcquireWithin(5 * time.Millisecond) {
		defer s.Release()
		// DO WORK...
	}

}

Written by Bruno Moura brunotm@gmail.com

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Sema

type Sema chan struct{}

Sema provides a simple semaphore implementation using a channel

func New

func New(cap int) (Sema, error)

New creates a new semaphore with the given maximum capacity for concurrent access. Will panic if cap < 1

func (Sema) Acquire

func (s Sema) Acquire()

Acquire the semaphore, will block if semaphore is full until any other holder release it. Will panic if semaphore is nil

func (Sema) AcquireWithin

func (s Sema) AcquireWithin(timeout time.Duration) bool

AcquireWithin the given timeout return true on success and false on failure Will panic if semaphore is nil

func (Sema) Cap

func (s Sema) Cap() int

Cap return semaphore capacity Will panic if semaphore is nil

func (Sema) Holders

func (s Sema) Holders() int

Holders return the current holders count Will panic if semaphore is nil

func (Sema) Release

func (s Sema) Release()

Release the semaphore Will panic if called on a non acquired semaphore

func (Sema) TryAcquire

func (s Sema) TryAcquire() bool

TryAcquire the semaphore without blocking return true on success and false on failure. Will panic if semaphore is nil

Jump to

Keyboard shortcuts

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