Skip to content

Commit 09e482d

Browse files
committed
Channel updates, documentation, and tests.
1 parent 6a76967 commit 09e482d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+1105
-329
lines changed

doc/channel.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ msg = messages.take
5757
puts msg
5858
```
5959

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`.
6161

6262
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.
6363

@@ -80,12 +80,12 @@ puts [x, y, x+y].join(' ')
8080

8181
## Channel Buffering
8282

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:
8484

8585
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.
8686

8787
```ruby
88-
ch = Concurrent::Channel.new(size: 2)
88+
ch = Concurrent::Channel.new(capacity: 2)
8989
ch << 1
9090
ch << 2
9191

@@ -95,7 +95,7 @@ puts ~ch
9595

9696
## Channel Synchronization
9797

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.
9999

100100
```ruby
101101
def worker(done_channel)
@@ -106,7 +106,7 @@ def worker(done_channel)
106106
done_channel << true
107107
end
108108

109-
done = Concurrent::Channel.new(size: 1)
109+
done = Concurrent::Channel.new(capacity: 1)
110110
Concurrent::Channel.go{ worker(done) }
111111

112112
~done # block until signaled
@@ -192,7 +192,7 @@ def fibonacci(n, c)
192192
c.close
193193
end
194194

195-
chan = Concurrent::Channel.new(size: 10)
195+
chan = Concurrent::Channel.new(capacity: 10)
196196
Concurrent::Channel.go { fibonacci(chan.capacity, c) }
197197
chan.each { |i| puts i }
198198
```

examples/a-tour-of-go-channels/buffered-channels.rb

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,15 @@
55
Channel = Concurrent::Channel
66

77
## A Tour of Go: Buffered Channels
8-
# https://tour.golang.org/concurrency/3
8+
# https://tour.golang.org/concurrency/3
99

10-
ch = Channel.new(size: 2)
10+
ch = Channel.new(capacity: 2)
1111
ch << 1
1212
ch << 2
1313

1414
puts ~ch
1515
puts ~ch
1616

17-
expected = <<-STDOUT
17+
__END__
1818
1
1919
2
20-
STDOUT

examples/a-tour-of-go-channels/channels.rb

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
Channel = Concurrent::Channel
66

77
## A Tour of Go: Channels
8-
# https://tour.golang.org/concurrency/2
8+
# https://tour.golang.org/concurrency/2
99

1010
def sum(a, c)
1111
sum = a.reduce(0, &:+)
@@ -22,6 +22,5 @@ def sum(a, c)
2222

2323
puts [x, y, x+y].join(' ')
2424

25-
expected = <<-STDOUT
25+
__END__
2626
-5 17 12
27-
STDOUT

examples/a-tour-of-go-channels/default-selection.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
end
2525
end
2626

27-
expected = <<-STDOUT
27+
__END__
2828
.
2929
.
3030
tick.
@@ -41,4 +41,3 @@
4141
.
4242
tick.
4343
BOOM!
44-
STDOUT

examples/a-tour-of-go-channels/equivalent-binary-trees.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@ def same(t1, t2)
6565
puts same(new_tree(1), new_tree(1))
6666
puts same(new_tree(1), new_tree(2))
6767

68-
expected = <<-STDOUT
68+
__END__
6969
true
7070
false
71-
STDOUT

examples/a-tour-of-go-channels/range-and-close.rb

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
Channel = Concurrent::Channel
66

77
## A Tour of Go: Range and Close
8-
# https://tour.golang.org/concurrency/4
8+
# https://tour.golang.org/concurrency/4
99

1010
def fibonacci(n, c)
1111
x, y = 0, 1
@@ -16,11 +16,11 @@ def fibonacci(n, c)
1616
c.close
1717
end
1818

19-
c = Channel.new(size: 10)
19+
c = Channel.new(capacity: 10)
2020
Channel.go { fibonacci(c.capacity, c) }
2121
c.each { |i| puts i }
2222

23-
expected = <<-STDOUT
23+
__END__
2424
0
2525
1
2626
1
@@ -31,4 +31,3 @@ def fibonacci(n, c)
3131
13
3232
21
3333
34
34-
STDOUT

examples/a-tour-of-go-channels/select.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def fibonacci(c, quit)
3030

3131
fibonacci(c, quit)
3232

33-
expected = <<-STDOUT
33+
__END__
3434
0
3535
1
3636
1
@@ -42,4 +42,3 @@ def fibonacci(c, quit)
4242
21
4343
34
4444
quit
45-
STDOUT

examples/go-by-example-channels/channel-buffering.rb

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,14 @@
77
## Go by Example: Channel Buffering
88
# https://gobyexample.com/channel-buffering
99

10-
messages = Channel.new(size: 2) # buffered
10+
messages = Channel.new(capacity: 2) # buffered
1111

1212
messages.put 'buffered'
1313
messages.put 'channel'
1414

1515
puts messages.take
1616
puts messages.take
1717

18-
expected = <<-STDOUT
18+
__END__
1919
buffered
2020
channel
21-
STDOUT

examples/go-by-example-channels/channel-directions.rb

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,13 @@ def pong(pings, pongs)
1919
pongs << msg
2020
end
2121

22-
pings = Channel.new(size: 1) # buffered
23-
pongs = Channel.new(size: 1) # buffered
22+
pings = Channel.new(capacity: 1) # buffered
23+
pongs = Channel.new(capacity: 1) # buffered
2424

2525
ping(pings, 'passed message')
2626
pong(pings, pongs)
2727

2828
puts ~pongs
2929

30-
expected = <<-STDOUT
30+
__END__
3131
passed message
32-
STDOUT

examples/go-by-example-channels/channel-synchronization.rb

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,11 @@ def worker(done_channel)
1515
done_channel << true # alias for `#put`
1616
end
1717

18-
done = Channel.new(size: 1) # buffered
18+
done = Channel.new(capacity: 1) # buffered
1919
Channel.go{ worker(done) }
2020

2121
~done # alias for `#take`
2222

23-
expected = <<-STDOUT
23+
__END__
2424
working...
2525
done
26-
STDOUT

0 commit comments

Comments
 (0)