Skip to content

Instantly share code, notes, and snippets.

@yumed15
Created November 22, 2023 12:14
Show Gist options
  • Save yumed15/95a3653df9ee3ebe2d3afbe6ad20d4b4 to your computer and use it in GitHub Desktop.
Save yumed15/95a3653df9ee3ebe2d3afbe6ad20d4b4 to your computer and use it in GitHub Desktop.
c := sync.NewCond(&sync.Mutex{})
queue := male([]interface{}, 0, 10)
removeFromQueue := func(delay time.Duration) {
time.Sleep(delay)
c.L.Lock()
queue = queue[1:]
c.L.Unlock()
c.Signal() // <- let a goroutine waiting for a condition to know that smth happened
}
for i:=0; i<10; i++ {
c.L.Lock()
for len(queue) == 2 {
c.Wait()
}
queue = append(queue, struct{}{})
go removeFromQueue(1*time.Second)
c.L.Unlock()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment