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
+6-6Lines changed: 6 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -57,7 +57,7 @@ msg = messages.take
57
57
puts msg
58
58
```
59
59
60
-
By default, channels are *unbuffered*, meaning that they have a size of zero and only accept puts and takes when both a putting and a taking thread are available. If a `put` is started when there is no taker thread the call will block. As soon as another thread calls `take` the exchange will occur and both calls will return on their respective threads. Similarly, is a `take` is started when there is no putting thread the call will block until another thread calls `put`.
60
+
By default, channels are *unbuffered*, meaning that they have a capacity of zero and only accept puts and takes when both a putting and a taking thread are available. If a `put` is started when there is no taker thread the call will block. As soon as another thread calls `take` the exchange will occur and both calls will return on their respective threads. Similarly, is a `take` is started when there is no putting thread the call will block until another thread calls `put`.
61
61
62
62
The following, slightly more complex example, concurrently sums two different halves of a list then combines the results. It uses an unbuffered channel to pass the results from the two goroutines back to the main thread. The main thread blocks on the two `take` calls until the worker goroutines are done. This example also uses the convenience aliases {#<<} and {#~}. Since channels in Go are part of the language, channel operations are performed using special channel operators rather than functions. These operators help clearly indicate that channel operations are being performed. The operator overloads `<<` for `put` and `~` for `take` help reinforce this idea in Ruby.
63
63
@@ -80,12 +80,12 @@ puts [x, y, x+y].join(' ')
80
80
81
81
## Channel Buffering
82
82
83
-
One common channel variation is a *buffered* channel. A buffered channel has a finite number of slots in the buffer which can be filled. Putting threads can put values into the channel even if there is no taking threads, up to the point where the buffer is filled. Once a buffer becomes full the normal blocking behavior resumes. A buffered channel is created by giving a `:size` option on channel creation:
83
+
One common channel variation is a *buffered* channel. A buffered channel has a finite number of slots in the buffer which can be filled. Putting threads can put values into the channel even if there is no taking threads, up to the point where the buffer is filled. Once a buffer becomes full the normal blocking behavior resumes. A buffered channel is created by giving a `:capacity` option on channel creation:
84
84
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::Channel.new(size:2)
88
+
ch =Concurrent::Channel.new(capacity:2)
89
89
ch <<1
90
90
ch <<2
91
91
@@ -95,7 +95,7 @@ puts ~ch
95
95
96
96
## Channel Synchronization
97
97
98
-
The main purpose of channels is to synchronize operations across goroutines. One common pattern for this is to created a `size: 1` buffered channel which is used to signal that work is complete. The following example calls a `worker` function on a goroutine and passes it a "done" channel. The main thread then calls `take` on the "done" channel and blocks until signaled.
98
+
The main purpose of channels is to synchronize operations across goroutines. One common pattern for this is to created a `capacity: 1` buffered channel which is used to signal that work is complete. The following example calls a `worker` function on a goroutine and passes it a "done" channel. The main thread then calls `take` on the "done" channel and blocks until signaled.
99
99
100
100
```ruby
101
101
defworker(done_channel)
@@ -106,7 +106,7 @@ def worker(done_channel)
106
106
done_channel <<true
107
107
end
108
108
109
-
done =Concurrent::Channel.new(size:1)
109
+
done =Concurrent::Channel.new(capacity:1)
110
110
Concurrent::Channel.go{ worker(done) }
111
111
112
112
~done # block until signaled
@@ -192,7 +192,7 @@ def fibonacci(n, c)
192
192
c.close
193
193
end
194
194
195
-
chan =Concurrent::Channel.new(size:10)
195
+
chan =Concurrent::Channel.new(capacity:10)
196
196
Concurrent::Channel.go { fibonacci(chan.capacity, c) }
0 commit comments