Skip to content

orkunkaraduman/go-criticalsection

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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)
}