mutex

package
v0.41.0 Latest Latest
Warning

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

Go to latest
Published: Feb 2, 2026 License: MIT Imports: 1 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Example

func Example()

Example 展示了 Mutex 的使用示例。 创建一个 Mutex 并在多个 goroutine 中使用,确保同一时间只有一个 goroutine 执行关键代码。

Types

type Mutex

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

Mutex 是一个互斥锁,同一时间仅允许一个线程访问共享资源。 它的实现基于容量为 1 的通道,类似于信号量。

func NewMutex

func NewMutex() *Mutex

NewMutex 创建一个新的 Mutex。 返回一个初始化的互斥锁实例。

func (*Mutex) Lock

func (mutex *Mutex) Lock()

Lock 获取互斥锁。 如果锁已被占用,则当前 goroutine 会阻塞直到锁被释放。

func (*Mutex) Unlock

func (mutex *Mutex) Unlock()

Unlock 释放互斥锁。 调用此方法会解除对锁的占用,允许其他 goroutine 获取锁。

Jump to

Keyboard shortcuts

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