package main import "time" type Fork chan struct{} func NewForks(n int) []Fork { forks := make([]Fork, n) for i := range forks { forks[i] = make(Fork, 1) forks[i].Put() } return forks } func (f Fork) Grab() { <-f } func (f Fork) TryGrab(d time.Duration) bool { select { case <-f: return true case <-time.After(d): return false } } func (f Fork) Put() { f <- struct{}{} }