umutex

package module
v0.0.0-...-18216d2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 17, 2015 License: MIT Imports: 0 Imported by: 113

README

Unblocking Mutex

This simple package provides unblocking mutexes for those who don't want to write many select clauses or get confused by numerous channels.

Usage Example

package main

import (
	"fmt"
	"github.com/yudai/umutex"
)

func main() {
	// Create mutex
	mutex := umutex.New()

	// First time, try should succeed
	if mutex.TryLock() {
		fmt.Println("SUCCESS")
	} else {
		fmt.Println("FAILURE")
	}

	// Second time, try should fail as it's locked
	if mutex.TryLock() {
		fmt.Println("SUCCESS")
	} else {
		fmt.Println("FAILURE")
	}

	// Unclock mutex
	mutex.Unlock()

	// Third time, try should succeed again
	if mutex.TryLock() {
		fmt.Println("SUCCESS")
	} else {
		fmt.Println("FAILURE")
	}
}

The output is;

SUCCESS
FAILURE
SUCCESS

ForceLock() method is also availale for normal blocking lock.

Documentation

Overview

Package umutex provides unblocking mutex

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type UnblockingMutex

type UnblockingMutex struct {
	// Raw channel
	C chan bool
}

UnblockingMutex represents an unblocking mutex.

func New

func New() *UnblockingMutex

New returnes a new unblocking mutex instance.

func (UnblockingMutex) ForceLock

func (m UnblockingMutex) ForceLock()

ForceLock surely locks the mutex, however, this function blocks when the mutex is locked at the time.

func (UnblockingMutex) TryLock

func (m UnblockingMutex) TryLock() (result bool)

TryLock tries to lock the mutex. When the mutex is free at the time, the function locks the mutex and return true. Otherwise false will be returned. In the both cases, this function doens't block and return the result immediately.

func (UnblockingMutex) Unlock

func (m UnblockingMutex) Unlock()

Unlock unclocks the mutex.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL