Giter Site home page Giter Site logo

papiguy / go-recursive-mutex Goto Github PK

View Code? Open in Web Editor NEW

This project forked from 90techsas/go-recursive-mutex

0.0 2.0 0.0 4 KB

recmutex is a tiny mutex lib for working with recursive mutex

License: BSD 2-Clause "Simplified" License

Go 100.00%

go-recursive-mutex's Introduction

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

go-recursive-mutex's People

Contributors

anchukhanna avatar nurza avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.