12 func random_between (begin, end int64) int64 {
13 random_byte := make([]byte, 1)
14 rand.Read(random_byte)
15 return int64(random_byte[0]) % (end - begin + 1) + begin
18 func random_sleep () {
19 time.Sleep(random_between(0, MAX_SLEEP_TIME) * 10e7)
22 func signal (channel chan int) {
23 if len(channel) < cap(channel) || cap(channel) == 0 {
28 func wait (channel chan int) {
41 front, rear, count int
46 func newMonitor () *Monitor {
49 make(chan int, BUFFER_SIZE),
50 make(chan int, BUFFER_SIZE),
51 make([]int, BUFFER_SIZE),
55 func (m *Monitor) produce (i int) {
57 for m.count == BUFFER_SIZE { // buffer cheio?
59 wait(m.empty) // (quase) uma variável de condição
62 Println("producing:", i)
64 m.rear = (m.rear + 1) % BUFFER_SIZE
70 func (m *Monitor) consume () {
72 for m.count == 0 { // buffer vazio?
74 wait(m.full) // (quase) uma variável de condição
77 v := m.buffer[m.front]
78 m.front = (m.front + 1) % BUFFER_SIZE
80 Println("\t\t\tconsuming:", v)
86 for i := 0; ; i++ { // laço infinito
93 for { // laço infinito
100 monitor = newMonitor()
103 wait(make(chan int)) // espera indefinidamente