-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path35_bufferred_channels.go
68 lines (57 loc) · 1.97 KB
/
35_bufferred_channels.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package main
import (
"fmt"
"sync"
)
/*
-Please refer to the snippet 30, 31, 32, 33, 34 to understand benefits of using channels
-This example accepts three values
-cChanSize --> identifies the buffered channel size
-cWorkerCount --> this defines the number of worker routines that are going to work on the channel
-cTaskCount --> this defines how many tasks that are going to be carried out
-In this example, taskLoader function will load the tasks that have to be carried out by the workers. When the program
starts, it initially starts loading the tasks into a buffered channel in a separate go routine. The idea is to start
workers to consume from the channel as soon as they become available.
-Buffered channels
A buffered channel is a channel with a capacity to hold. You can think of a channel as a pipe, where a go routine
checks an incoming stream of items. In the case of buffered channels, there can be multiple items in the channel for
a given time. If the channel is empty, all the go routines that are looking for incoming items from the channel are
blocked until there is an item available in the channel.
Multiple go routines can receive items from a buffered channel one after another without waiting for other go routines
to finish.
*/
const (
cChanSize int = 20
cWorkerCount int = 5
cTaskCount int = 20000
)
var tasks chan int
var wg sync.WaitGroup
func main() {
// create counting semaphore to wait until the counting process is finished
wg.Add(cWorkerCount)
// this creates a buffered channel with type int and size of chanSize
tasks = make(chan int, cChanSize)
go taskLoader()
for i := 1; i <= cWorkerCount; i++ {
go worker(i)
}
wg.Wait()
}
func taskLoader() {
for i := 1; i <= cTaskCount; i++ {
tasks <- i
}
close(tasks)
}
func worker(workerNum int) {
defer wg.Done()
for {
task, ok := <- tasks
if !ok {
fmt.Printf("worker %v shutting down\n", workerNum)
break
}
fmt.Printf("worker %v processing task num %v\n", workerNum, task)
}
}