gsema

package module
v0.0.0-...-7f6a61b Latest Latest
Warning

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

Go to latest
Published: Jan 20, 2019 License: MIT Imports: 1 Imported by: 6

README

gsema

a simple goroutine limit pool.

Installation

$ go get github.com/EDDYCJY/gsema

Usage

package main

import (
	"fmt"
	"time"

	"github.com/EDDYCJY/gsema"
)

var sema = gsema.NewSemaphore(3)

func main() {
	userCount := 10
	for i := 0; i < userCount; i++ {
		go Read(i)
	}

	sema.Wait()
}

func Read(i int) {
	defer sema.Done()
	sema.Add(1)

	fmt.Printf("go func: %d, time: %d\n", i, time.Now().Unix())
	time.Sleep(time.Second)
}

Output

...
go func: 0, time: 1547880322
go func: 2, time: 1547880322
go func: 1, time: 1547880322
go func: 3, time: 1547880323
go func: 4, time: 1547880323
go func: 5, time: 1547880323
go func: 7, time: 1547880324
go func: 8, time: 1547880324
go func: 6, time: 1547880324
go func: 9, time: 1547880325

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Semaphore

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

func NewSemaphore

func NewSemaphore(maxSize int) *Semaphore

func (*Semaphore) Add

func (s *Semaphore) Add(delta int)

func (*Semaphore) Done

func (s *Semaphore) Done()

func (*Semaphore) Wait

func (s *Semaphore) Wait()

Jump to

Keyboard shortcuts

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