Skip to content

recmutex is a tiny mutex lib for working with recursive mutex

License

Notifications You must be signed in to change notification settings

90TechSAS/go-recursive-mutex

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 

Repository files navigation

go-recursive-mutex (recmutex)

Recmutex is a tiny mutex lib working with goroutine's id for recursive mutex

Installation

go get -u github.com/huandu/goroutine
go get -u github.com/90TechSAS/go-recursive-mutex

Recursive locking with native mutex

This is a simple example of recursive locking with native mutex

package main

import (
	"sync"
)

type Struct struct{ sync.Mutex }

func main() {
	var s Struct
	s.A()
}

func (s *Struct) A() {
	s.Lock()
	s.B()
	s.Unlock()
}

func (s *Struct) B() {
	s.Lock()
	s.Unlock()
}

Obviously, this code doesn't work and make a beautiful error: fatal error: all goroutines are asleep - deadlock!

Multi locking with recmutex

Recmutex allow you to do recursive mutex with Go!

package main

import (
	"github.com/90TechSAS/go-recursive-mutex"
)

type Struct struct{ recmutex.RecursiveMutex }

func main() {
	var s Struct
	s.A()
}

func (s *Struct) A() {
	s.Lock()
	s.B()
	s.Unlock()
}

func (s *Struct) B() {
	s.Lock()
	s.Unlock()
}

Benchmarking

$ go run examples/benchmark_narive-vs-recursive.go 
[Native Mutex] Elapsed: 4.374499979s / 2285975 locks/sec / counter: 10000000
[Recursive Mutex] Elapsed: 3.854341015s / 2594477 locks/sec / counter: 10000000

Releases

No releases published

Packages

No packages published

Languages