You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: doc/channel.md
+29-29Lines changed: 29 additions & 29 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -28,7 +28,7 @@ The Go programming languages uses "goroutines" as the core concurrency mechanism
28
28
```ruby
29
29
puts"Main thread: #{Thread.current}"
30
30
31
-
Concurrent::Edge::Channel.go do
31
+
Concurrent::Channel.go do
32
32
puts"Goroutine thread: #{Thread.current}"
33
33
end
34
34
@@ -47,9 +47,9 @@ The core channel operations are {#put} and {#take} (aliased as {#send} and {#rec
47
47
The following, simple example creates a channel, launches a goroutine from which a value is placed into the channel, then reads that value from the channel. When run this example will display "ping" in the console.
48
48
49
49
```ruby
50
-
messages =Concurrent::Edge::Channel.new
50
+
messages =Concurrent::Channel.new
51
51
52
-
Concurrent::Edge::Channel.go do
52
+
Concurrent::Channel.go do
53
53
messages.put 'ping'
54
54
end
55
55
@@ -69,10 +69,10 @@ end
69
69
70
70
a = [7, 2, 8, -9, 4, 0]
71
71
l = a.length /2
72
-
c =Concurrent::Edge::Channel.new
72
+
c =Concurrent::Channel.new
73
73
74
-
Concurrent::Edge::Channel.go { sum(a[-l, l], c) }
75
-
Concurrent::Edge::Channel.go { sum(a[0, l], c) }
74
+
Concurrent::Channel.go { sum(a[-l, l], c) }
75
+
Concurrent::Channel.go { sum(a[0, l], c) }
76
76
x, y =~c, ~c # `~` is an alias for `take` or `receive`
77
77
78
78
puts [x, y, x+y].join('')
@@ -85,7 +85,7 @@ One common channel variation is a *buffered* channel. A buffered channel has a f
85
85
The following example creates a buffered channel with two slots. It then makes two `put` calls, adding values to the channel. These calls do not block because the buffer has room. Were a third `put` call to be made before an `take` calls, the third `put` would block.
86
86
87
87
```ruby
88
-
ch =Concurrent::Edge::Channel.new(size:2)
88
+
ch =Concurrent::Channel.new(size:2)
89
89
ch <<1
90
90
ch <<2
91
91
@@ -106,8 +106,8 @@ def worker(done_channel)
106
106
done_channel <<true
107
107
end
108
108
109
-
done =Concurrent::Edge::Channel.new(size:1)
110
-
Concurrent::Edge::Channel.go{ worker(done) }
109
+
done =Concurrent::Channel.new(size:1)
110
+
Concurrent::Channel.go{ worker(done) }
111
111
112
112
~done # block until signaled
113
113
```
@@ -119,21 +119,21 @@ Often it is necessary for a single thread to operate on more than one channel. T
119
119
The following example spawns two goroutines, each of which goes to sleep before putting a value onto a channel. The main thread loops twice over a `select` and, in each loop, takes a value off of whichever channel returns one first.
120
120
121
121
```ruby
122
-
c1 =Concurrent::Edge::Channel.new
123
-
c2 =Concurrent::Edge::Channel.new
122
+
c1 =Concurrent::Channel.new
123
+
c2 =Concurrent::Channel.new
124
124
125
-
Concurrent::Edge::Channel.go do
125
+
Concurrent::Channel.go do
126
126
sleep(1)
127
127
c1 <<'one'
128
128
end
129
129
130
-
Concurrent::Edge::Channel.go do
130
+
Concurrent::Channel.go do
131
131
sleep(2)
132
132
c1 <<'two'
133
133
end
134
134
135
135
2.times do
136
-
Concurrent::Edge::Channel.selectdo |s|
136
+
Concurrent::Channel.selectdo |s|
137
137
s.take(c1) { |msg| print"received #{msg}\n" }
138
138
s.take(c2) { |msg| print"received #{msg}\n" }
139
139
end
@@ -153,7 +153,7 @@ The next example calculates the first 10 fibonacci numbers, passing them to the
153
153
deffibonacci(c, quit)
154
154
x, y =0, 1
155
155
loopdo
156
-
Concurrent::Edge::Channel.selectdo |s|
156
+
Concurrent::Channel.selectdo |s|
157
157
s.case(c, :<<, x) { x, y = y, x+y; x } # alias for `s.put`
158
158
s.case(quit, :~) do# alias for `s.take`
159
159
puts'quit'
@@ -163,10 +163,10 @@ def fibonacci(c, quit)
163
163
end
164
164
end
165
165
166
-
c =Concurrent::Edge::Channel.new
167
-
quit =Concurrent::Edge::Channel.new
166
+
c =Concurrent::Channel.new
167
+
quit =Concurrent::Channel.new
168
168
169
-
Concurrent::Edge::Channel.go do
169
+
Concurrent::Channel.go do
170
170
10.times { puts~c }
171
171
quit <<0
172
172
end
@@ -192,8 +192,8 @@ def fibonacci(n, c)
192
192
c.close
193
193
end
194
194
195
-
chan =Concurrent::Edge::Channel.new(size:10)
196
-
Concurrent::Edge::Channel.go { fibonacci(chan.capacity, c) }
195
+
chan =Concurrent::Channel.new(size:10)
196
+
Concurrent::Channel.go { fibonacci(chan.capacity, c) }
197
197
chan.each { |i| puts i }
198
198
```
199
199
@@ -203,16 +203,16 @@ chan.each { |i| puts i }
203
203
204
204
A {.timer} is a specialized channel which triggers at a predefined time, specified as a number of seconds in the future. It is similar in concept to a {Concurrent::ScheduledTask} but operates as a channel and can fully participate in all channel operations.
205
205
206
-
The following code example creates two timers with different delay values. The first timer is allowed to expire (trigger) by having the main thread perform a `take` on it. When the timer expires it puts a {Concurrent::Edge::Channel::Tick} object into its buffer and closes. The second timer is listened to on a goroutine but the it never expires: the main thread stops (closes) the timer before it expires. Note that the goroutine in this example blocks forever and never exits. Since the timer is closed it never puts the `Tick` into its buffer.
206
+
The following code example creates two timers with different delay values. The first timer is allowed to expire (trigger) by having the main thread perform a `take` on it. When the timer expires it puts a {Concurrent::Channel::Tick} object into its buffer and closes. The second timer is listened to on a goroutine but the it never expires: the main thread stops (closes) the timer before it expires. Note that the goroutine in this example blocks forever and never exits. Since the timer is closed it never puts the `Tick` into its buffer.
207
207
208
208
```ruby
209
-
timer1 =Concurrent::Edge::Channel.timer(2)
209
+
timer1 =Concurrent::Channel.timer(2)
210
210
211
211
~timer1
212
212
puts'Timer 1 expired'
213
213
214
-
timer2 =Concurrent::Edge::Channel.timer(1)
215
-
Concurrent::Edge::Channel.go do
214
+
timer2 =Concurrent::Channel.timer(1)
215
+
Concurrent::Channel.go do
216
216
~timer2
217
217
print"Timer 2 expired\n"
218
218
end
@@ -226,8 +226,8 @@ A {.ticker} is a specialized channel which triggers over and over again at a pre
226
226
The following example creates a ticker which triggers every half-second. A goroutine iterates over the ticker using the `each` method, printing the tick at every interval. When the main thread stops (closes) the ticker the `each` call ends and the goroutine exits.
227
227
228
228
```ruby
229
-
ticker =Concurrent::Edge::Channel.ticker(0.5)
230
-
Concurrent::Edge::Channel.go do
229
+
ticker =Concurrent::Channel.ticker(0.5)
230
+
Concurrent::Channel.go do
231
231
ticker.each do |tick|
232
232
print"Tick at #{tick}\n"
233
233
end
@@ -243,11 +243,11 @@ print "Ticker stopped\n"
243
243
As with a Ruby `case` statement, a `Channel.select` statement will accept a `default` clause which will trigger if none of the other clauses trigger. Not surprisingly, the `default` clause must be the last clause in a `select` block.
244
244
245
245
```ruby
246
-
tick =Concurrent::Edge::Channel.tick(0.1) # alias for `ticker`
247
-
boom =Concurrent::Edge::Channel.after(0.5) # alias for `timer`
246
+
tick =Concurrent::Channel.tick(0.1) # alias for `ticker`
247
+
boom =Concurrent::Channel.after(0.5) # alias for `timer`
0 commit comments