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

utils

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Oct 10, 2026 License: MIT Imports: 2 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Once

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

Once 是 sync.Once 的增强版本: 除了保证函数只执行一次以外, 还通过返回值告诉调用方"这一次执行是否由我触发".

与 sync.Once 不同, 它额外提供 Reset 以便复用. 语义保证:

  • 第一个调用 Do 的 goroutine 执行 do 并返回 true;
  • 在此之后所有调用方都返回 false, 且能看到 do 产生的内存写入 (Do 返回时存在 happens-before 关系);
  • 当 do panic 时, 标记不会置位, 下一次调用会重新尝试执行.

func (*Once) Do

func (o *Once) Do(do func()) bool

Do 执行 do 最多一次. 返回 true 表示本次调用真正执行了 do.

func (*Once) Done

func (o *Once) Done() bool

Done 返回是否已经执行过.

func (*Once) Reset

func (o *Once) Reset()

Reset 重置状态, 使得 Do 可以再次执行. 注意: 与 Reset 并发的 Do 调用语义等价于"重新开始一轮", 不应依赖其顺序.

type OnceNoWait

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

OnceNoWait 是无锁(基于原子操作)的一次性执行器. 适用于"只允许一个赢家, 失败/重复调用无需等待"的场景, 例如资源注销、信号只发一次.

语义与 Once 的差异:

  • 非阻塞: CAS 失败立即返回 false, 不会等待赢家执行完成;
  • 因此调用方不能依赖"返回 false 时 do 已经执行完毕"的可见性保证.

func (*OnceNoWait) Do

func (o *OnceNoWait) Do(do func()) bool

Do 尝试抢占执行权. 返回 true 表示由本次调用执行 do.

func (*OnceNoWait) Done

func (o *OnceNoWait) Done() bool

Done 返回是否已被抢占.

func (*OnceNoWait) Reset

func (o *OnceNoWait) Reset()

Reset 重置以便重新使用.

Jump to

Keyboard shortcuts

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