semaphore

package module
v0.0.0-...-a5135ed Latest Latest
Warning

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

Go to latest
Published: May 13, 2021 License: Apache-2.0 Imports: 0 Imported by: 0

README

go-semaphore

Unfortunately, Golang doesn’t has built in Semaphore implementation. So, here you can find an implementation of Semaphore in Go.

Example
package main

import (
	"fmt"
	"time"

	"github.com/iqnev/go-semaphore"
)

func main() {

	semaphore := semaphore.NewSemaphore(2)

	doneS := make(chan bool, 1)

	proccess := 13

	for i := 1; i <= proccess; i++ {
		semaphore.Acquire()

		go func(p int) {
			defer semaphore.Release()
			runningProcess(p)
			if p == proccess {
				doneS <- true
			}
		}(i)
	}

	<-doneS
}

func runningProcess(ID int) {
	fmt.Println(time.Now().Format("15:04:05.000"), "Running task with ID", ID)
	time.Sleep(5 * time.Second)
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewSemaphore

func NewSemaphore(permits int) *semaphore

Types

type Semaphore

type Semaphore interface {
	Acquire()
	Release()
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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