concurrent

package
v0.20.0 Latest Latest
Warning

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

Go to latest
Published: Apr 26, 2024 License: Apache-2.0 Imports: 1 Imported by: 0

Documentation

Overview

Concurrent utilities

Example (ChanSemaphore)
sema := NewChanSemaphore(100) // new a semaphore with 100 permits
sema.Acquire()                // will be block if no permits available
defer sema.Release()          // return permit

if sema.TryAcquire() { // will return false if no permits available
	defer sema.Release() // return permit
}
Output:

Example (LockSemaphore)
sema := NewLockSemaphore(100) // new a semaphore with 100 permits
sema.Acquire()                // will be block if no permits available
defer sema.Release()          // return permit

if sema.TryAcquire() { // will return false if no permits available
	defer sema.Release() // return permit
}
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ChanSemaphore

type ChanSemaphore struct {
	// contains filtered or unexported fields
}

A Semaphore implementation using channel

func NewChanSemaphore

func NewChanSemaphore(permits int64) *ChanSemaphore

New a ChanSemaphore.

permits: max permits this semaphore could be acquired.

func (*ChanSemaphore) Acquire

func (s *ChanSemaphore) Acquire()

func (*ChanSemaphore) Release

func (s *ChanSemaphore) Release()

func (*ChanSemaphore) TryAcquire

func (s *ChanSemaphore) TryAcquire() bool

type LockSemaphore

type LockSemaphore struct {
	// contains filtered or unexported fields
}

A Semaphore implementation using sync.Mutex

func NewLockSemaphore

func NewLockSemaphore(permits int64) *LockSemaphore

New a LockSemaphore.

permits: max permits this semaphore could be acquired.

func (*LockSemaphore) Acquire

func (s *LockSemaphore) Acquire()

func (*LockSemaphore) Release

func (s *LockSemaphore) Release()

func (*LockSemaphore) TryAcquire

func (s *LockSemaphore) TryAcquire() bool

type Semaphore

type Semaphore interface {
	// Acquire a permit from this semaphore, blocking until one is available.
	Acquire()
	// Return a permit to the semaphore.
	// Over release will not make the available permits exceeds capacity.
	Release()
	// Acquire a permit from this semaphore, only if one is available at the time of invocation,
	// never blocking.
	TryAcquire() bool
}

Semaphore

Jump to

Keyboard shortcuts

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