Skip to content

TheCount/go-multilocker

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 

Repository files navigation

multilocker

Documentation

Install

go get github.com/TheCount/go-multilocker/multilocker

Usage

For the detailed API, see the Documentation.

Example:

var mtx1, mtx2 sync.Mutex

// goroutine 1
go func() {
  ml := multilocker.New(&mtx1, &mtx2)
  ml.Lock()
  ml.Unlock()
}()

// goroutine 2
go func() {
  ml := multilocker.New(&mtx2, &mtx1)
  ml.Lock()
  ml.Unlock()
}()

This example would be prone to deadlocks if goroutine 1 locked first mtx1, then mtx2, while goroutine 2 attempted to lock mtx2 first, and then mtx1. The use of a multilocker makes locking and unlocking atomic and deadlock-free in this example.

About

Lock multiple sync.Lockers atomically and deadlock-free

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages