GithubHelp home page GithubHelp logo

ywanbing / lock Goto Github PK

View Code? Open in Web Editor NEW
1.0 1.0 0.0 10 KB

内部记录了是谁在持有锁,在解锁时会打印使用锁的时间。 Internally records who is holding the lock, and prints the time when the lock is used when unlocking.

License: MIT License

Go 100.00%

lock's Introduction

lock

内部记录了是谁在持有锁,在解锁时会打印使用锁的时间。

Internally records who is holding the lock, and prints the time when the lock is used when unlocking.

Feature 特性

go.1.18 使用sync.mutex封装,带有获取锁超时的额外功能。

go.1.18 Wrapped with sync.mutex, with additional functionality for acquiring lock timeouts.

小于go1.18 的版本采用原子操作进行封装,对于锁的使用应该更快,当前是在通常情况下。

Versions smaller than go1.18 are encapsulated with atomic operations, and the use of locks should be faster, which is currently the usual case.

HowToUse 使用方式

example: use mutex

package main

import "github.com/ywanbing/lock"

func main() {
    mu := lock.NewDefMutex()
    mu.Lock("lock1")
    defer mu.Unlock()
    
    // do something ...
}

example: use mutex with timeOut

package main

import (
	"time"

	"github.com/ywanbing/lock"
)

func main() {
    mu := lock.NewDefMutex()
    
    if !mu.LockWithTimeOut("lock1", 1*time.Second) {
        return
    }
    	
    defer mu.Unlock()
    // do something ...
}

example: use self log component

package main

import (
	"github.com/ywanbing/lock"
)

func main() {
    // Implement lock.Logger to replace it with your own log
    log := lock.NewLog()
    mu := lock.NewMutex(log)
    
    mu.Lock("lock1")
    defer mu.Unlock()
    // do something  	
}

lock's People

Contributors

ywanbing avatar

Stargazers

 avatar

Watchers

 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.