criticalsection

package module
v0.0.0-...-80e6db4 Latest Latest
Warning

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

Go to latest
Published: Dec 25, 2018 License: BSD-3-Clause Imports: 6 Imported by: 0

README

DISCONTINUED

Please visit: github.com/orkunkaraduman/go-syncex

Go CriticalSection

GoDoc

The repository provides criticalsection package.

A CriticalSection is a kind of lock like mutex. But it doesn't block first locked goroutine/section again.

Example

package main

import (
	"fmt"
	"time"

	"github.com/orkunkaraduman/go-criticalsection"
)

func main() {
	var cs criticalsection.CriticalSection
	var f int
	cs.Lock()
	for i := 0; i < 5; i++ {
		cs.Lock()
		go func() {
			cs.Lock()
			f++
			fmt.Println("goroutine: ", f)
			cs.Unlock()
		}()
		f++
		fmt.Println("forloop: ", f)
		cs.Unlock()
	}
	fmt.Println("mainfunc: ", f)
	cs.Unlock()
	time.Sleep(1 * time.Second)
	fmt.Println("mainfunc: ", f)
}

Documentation

Overview

Package criticalsection is discontinued. Please visit: github.com/orkunkaraduman/go-syncex

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNotLocked = errors.New("not locked")
)

Library specific errors.

Functions

This section is empty.

Types

type CriticalSection

type CriticalSection struct {
	// contains filtered or unexported fields
}

A CriticalSection is a kind of lock like mutex. But it doesn't block first locked goroutine/section again.

A CriticalSection must not be copied after first use.

func (*CriticalSection) Lock

func (cs *CriticalSection) Lock()

Lock locks cs. If the lock is already in use different goroutine, the different goroutine blocks until the CriticalSection is available.

func (*CriticalSection) LockSection

func (cs *CriticalSection) LockSection(sc Section)

LockSection locks cs by given section. LockSection is faster than Lock(). If the lock is already in use different section, the different section blocks until the CriticalSection is available.

func (*CriticalSection) Unlock

func (cs *CriticalSection) Unlock()

Unlock unlocks cs. It panics if cs is not locked on entry to Unlock.

type Section

type Section uint64

A Section is an identifier of sections.

func NewSection

func NewSection() Section

NewSection creates a new Section.

Jump to

Keyboard shortcuts

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