semaphore

package
v0.27.0 Latest Latest
Warning

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

Go to latest
Published: Dec 10, 2025 License: MIT Imports: 2 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Example

func Example()

Example 展示了 Semaphore 的使用示例。 创建一个 Semaphore 并限制同时运行的 goroutine 数量。

Types

type Semaphore

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

Semaphore 是一个信号量,用于限制同时访问共享资源的 goroutine 数量。 它的实现基于带缓冲的通道。

func NewSemaphore

func NewSemaphore(capacity int) *Semaphore

NewSemaphore 创建一个新的 Semaphore。 参数 capacity 指定信号量的容量,即允许同时访问的最大 goroutine 数量。

func (*Semaphore) Acquire

func (sem *Semaphore) Acquire()

Acquire 获取信号量。 如果信号量已满,调用此方法的 goroutine 会阻塞直到有可用的容量。

func (*Semaphore) Release

func (sem *Semaphore) Release()

Release 释放信号量。 调用此方法会释放一个信号量容量,允许其他 goroutine 获取。

func (*Semaphore) TryAcquire

func (sem *Semaphore) TryAcquire() bool

type SemaphoreByCond

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

func NewSemaphoreByCond

func NewSemaphoreByCond(capacity int) *SemaphoreByCond

func (*SemaphoreByCond) Acquire

func (sm *SemaphoreByCond) Acquire()

func (*SemaphoreByCond) Release

func (sm *SemaphoreByCond) Release()

func (*SemaphoreByCond) TryAcquire

func (sm *SemaphoreByCond) TryAcquire() bool

Jump to

Keyboard shortcuts

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