Coder Social home page Coder Social logo

semaphore's Introduction

semaphore

Semaphore in Go

Build Status GoDoc Go Report Card

Usage

Initiate

import "github.com/abiosoft/semaphore"
...
sem := semaphore.New(5) // new semaphore with 5 permits

Acquire

sem.Acquire() // one
sem.AcquireMany(n) // multiple
sem.AcquireWithin(n, time.Second * 5) // timeout after 5 sec
sem.AcquireContext(ctx, n) // acquire with context

Release

sem.Release() // one
sem.ReleaseMany(n) // multiple

documentation

http://godoc.org/github.com/abiosoft/semaphore

semaphore's People

Contributors

abiosoft avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

semaphore's Issues

data race when running tests

Run with "go test -race ./..."

==================
WARNING: DATA RACE
Read at 0x00c4200742d8 by goroutine 7:
  github.com/kevinburke/semaphore.(*Semaphore).Release()
      /Users/kevin/src/github.com/kevinburke/semaphore/semaphore.go:95 +0xde
  github.com/kevinburke/semaphore.(*Semaphore).ReleaseMany()
      /Users/kevin/src/github.com/kevinburke/semaphore/semaphore.go:104 +0x61
  github.com/kevinburke/semaphore.aq()
      /Users/kevin/src/github.com/kevinburke/semaphore/semaphore_test.go:44 +0x37e

Previous write at 0x00c4200742d8 by goroutine 12:
  github.com/kevinburke/semaphore.(*Semaphore).AcquireMany()
      /Users/kevin/src/github.com/kevinburke/semaphore/semaphore.go:57 +0x103
  github.com/kevinburke/semaphore.aq()
      /Users/kevin/src/github.com/kevinburke/semaphore/semaphore_test.go:41 +0x1d9

Goroutine 7 (running) created at:
  github.com/kevinburke/semaphore.Test()
      /Users/kevin/src/github.com/kevinburke/semaphore/semaphore_test.go:18 +0xae
  testing.tRunner()
      /Users/kevin/go/src/testing/testing.go:746 +0x16c

Goroutine 12 (running) created at:
  github.com/kevinburke/semaphore.Test()
      /Users/kevin/src/github.com/kevinburke/semaphore/semaphore_test.go:18 +0xae
  testing.tRunner()
      /Users/kevin/go/src/testing/testing.go:746 +0x16c

A potential race condition

Acquire is ch <- 1 and release is <- ch. According to the Memory Model, there is no guarantee that a buffered channel receiving happens before the corresponding sending.
So I think the Acquire is <-ch while release is ch <- 1.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.