-
Notifications
You must be signed in to change notification settings - Fork 162
/
main.go
43 lines (32 loc) · 1.02 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package main
import (
"fmt"
"sync"
"time"
)
// a goroutine that is waiting for a signal, and a goroutine that is sending signals.
// Say we have a queue of fixed length 2, and 10 items we want to push onto the queue
func main() {
c := sync.NewCond(&sync.Mutex{})
queue := make([]interface{}, 0, 10)
removeFromQueue := func(delay time.Duration) {
time.Sleep(delay)
c.L.Lock() // critical section
queue = queue[1:]
fmt.Println("Removed from queue")
c.L.Unlock()
c.Signal() // let a goroutine waiting on the condition know that something has ocurred
}
for i := 0; i < 10; i++ {
c.L.Lock() // critical section
// When the queue is equal to two the main goroutine is suspend
// until a signal on the condition has been sent
for len(queue) == 2 {
c.Wait()
}
fmt.Println("Adding to queue")
queue = append(queue, struct{}{})
go removeFromQueue(1 * time.Second)
c.L.Unlock()
}
}