TheCount / go-multilocker

Lock multiple sync.Lockers atomically and deadlock-free

Repository from Github https://github.comTheCount/go-multilockerRepository from Github https://github.comTheCount/go-multilocker

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

License:MIT License


Languages

Language:Go 100.0%