mutex

package module
v0.0.0-...-a389356 Latest Latest
Warning

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

Go to latest
Published: May 14, 2023 License: MIT Imports: 3 Imported by: 1

README

A "safe" mutex lock

Safe means that there's no deadlock.

Example

var mu sync.Mutex

mu.Lock()

More Code....

// deadlock! I don't know why Go runtime will not panic.
mu.Lock()

However, the project can prevent that.

var mu Mutex

mu.Lock()

More Code....

// safe to lock again. Actually it doesn't lock.
mu.Lock()

But this will cause the chaos of lock state.

So another function will be useful.

IsLocked()

var mu Mutex

mu.Lock()

More Code....

if !mu.Locked() {
    mu.Lock()
}

What about unlock?

To avoid the chaos of lock state, there's a TryUnlock()

If you can't ensure the lock is locked or not.

Use TryUnlock()

It's safe to call concurrently(Of course!).

Recursive lock

Recursive lock acts like C++ STL Recursive lock.

It can lock multiple times, but if you need unlock, you need to unlock it multiple times. Or it will cause deadlock.

var rm Recursive
for i := 0; i < 5; i++ {
    rm.Lock()
}

More Code ...

for i := 0; i < 5; i++ {
    rm.Unlock()
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Mutex

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

func (*Mutex) IsBusy

func (m *Mutex) IsBusy() bool

func (*Mutex) IsLocked

func (m *Mutex) IsLocked() bool

func (*Mutex) Lock

func (m *Mutex) Lock()

func (*Mutex) TryLock

func (m *Mutex) TryLock() bool

func (*Mutex) TryUnlock

func (m *Mutex) TryUnlock() bool

func (*Mutex) Unlock

func (m *Mutex) Unlock()

type Recursive

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

func (*Recursive) Lock

func (r *Recursive) Lock()

func (*Recursive) TryLock

func (r *Recursive) TryLock() bool

func (*Recursive) Unlock

func (r *Recursive) Unlock()

Jump to

Keyboard shortcuts

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