Popularity
1.7
Growing
Activity
6.5
Growing
20
0
0

Description

go-lock is a Golang library implementing an efficient read-write lock with the following built-in mechanism: - Mutex - Trylock - No-starve read-write solution

Native sync/Mutex and sync/RWMutex are very powerful and reliable. However, it became a disaster if the lock was not released as expected. Or someone was holding the lock too long at the peak time. It slowed down whole system. Dealing with those cases, go-lock implements TryLock, TryLockWithTimeout and TryLockWithContext function in addition to Lock and Unlock. It provides flexibility to control the resources.

Programming language: Go
Tags: Go Tools     Go     Golang     Mutex     Trylock     Spinlock     Readwritelock    
Latest version: v1.0.1

go-lock alternatives and similar packages

Based on the "Go Tools" category

Do you think we are missing an alternative of go-lock or a related project?

Add another 'Go Tools' Package

README

go-lock

GoDoc GoDev Build Status Go Report Card Codecov Coverage Status License Mentioned in Awesome Go

go-lock is a Golang library implementing an effcient read-write lock with the following built-in mechanism:

  • Mutex
  • Trylock
  • No-starve read-write solution

Native sync/Mutex and sync/RWMutex are very powerful and reliable. However, it became a disaster if the lock was not released as expected. Or someone was holding the lock too long at the peak time. It slowed down whole system. Dealing with those cases, go-lock implements TryLock, TryLockWithTimeout and TryLockWithContext function in addition to Lock and Unlock. It provides flexibility to control the resources.

Installation

go get github.com/viney-shih/go-lock

Example

package main

import (
    "fmt"
    "sync/atomic"
    "time"

    lock "github.com/viney-shih/go-lock"
)

func main() {
    // initialized with default value
    casMut := lock.NewCASMutex()
    count := int32(0)

    casMut.Lock()
    go func() {
        time.Sleep(50 * time.Millisecond)
        fmt.Println("Now is", atomic.AddInt32(&count, 1)) // Now is 1
        casMut.Unlock()
    }()

    casMut.Lock()
    fmt.Println("Now is", atomic.AddInt32(&count, 2)) // Now is 3

    // TryLock without blocking
    fmt.Println("Return", casMut.TryLock()) // Return false

    // RTryLockWithTimeout without blocking
    fmt.Println("Return", casMut.RTryLockWithTimeout(50*time.Millisecond)) // Return false
    casMut.Unlock()

    // Output:
    // Now is 1
    // Now is 3
    // Return false
    // Return false
}

References

License

Apache-2.0


*Note that all licence references and agreements mentioned in the go-lock README section above are relevant to that project's source code only.