package
Version:
v0.39.0
Opens a new window with list of versions in this module.
Published: Jan 29, 2026
License: MIT
Opens a new window with license information.
Imports: 1
Opens a new window with list of imports.
Imported by: 0
Opens a new window with list of known importers.
Documentation
¶
Example 展示了 Mutex 的使用示例。
创建一个 Mutex 并在多个 goroutine 中使用,确保同一时间只有一个 goroutine 执行关键代码。
Mutex 是一个互斥锁,同一时间仅允许一个线程访问共享资源。
它的实现基于容量为 1 的通道,类似于信号量。
NewMutex 创建一个新的 Mutex。
返回一个初始化的互斥锁实例。
func (mutex *Mutex) Lock()
Lock 获取互斥锁。
如果锁已被占用,则当前 goroutine 会阻塞直到锁被释放。
func (mutex *Mutex) Unlock()
Unlock 释放互斥锁。
调用此方法会解除对锁的占用,允许其他 goroutine 获取锁。
Source Files
¶
Click to show internal directories.
Click to hide internal directories.