yudai / nmutex

Go Mutex With Names like a Map

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Named Mutex

Named Mutex is a simple package that provides a mutex map.

Instalation

go get -u github.com/yudai/nmutex

Example

	m := nmutex.New()

	releaseABC := m.Lock("abc") // you can get a lock

	releaseXYZ := m.Lock("xyz") // you can get a lock with another name
	defer releaseXYZ()          // using defer to release locks is a best practice

	go func() {
		// this call is blocked until releaseABC() has been called
		releaseABC := m.Lock("abc")
		defer releaseABC()
	}()

	releaseABC() // release the first lock
	// the goroutine above gets the lock at this timing

When the lock for a name has been released and there is no other gouroutines waiting for the lock, the internal resource for the name is automatically released.

About

Go Mutex With Names like a Map

License:MIT License


Languages

Language:Go 100.0%