@@ -16,14 +16,14 @@ threads = Array.new(3) { |i| Thread.new { ch.push message: i } }
1616sleep 0.01 # let the threads run
1717threads
1818# => [#<Thread:0x000003@channel.in.md:14 dead>,
19- # #<Thread:0x000004@channel.in.md:14 dead >,
20- # #<Thread:0x000005@channel.in.md:14 sleep_forever >]
19+ # #<Thread:0x000004@channel.in.md:14 sleep_forever >,
20+ # #<Thread:0x000005@channel.in.md:14 dead >]
2121```
2222
2323When message is popped the last thread continues and finishes as well.
2424
2525``` ruby
26- ch.pop # => {:message=>0 }
26+ ch.pop # => {:message=>2 }
2727threads.map(& :join )
2828# => [#<Thread:0x000003@channel.in.md:14 dead>,
2929# #<Thread:0x000004@channel.in.md:14 dead>,
@@ -38,14 +38,11 @@ one will be blocked until new messages is pushed.
3838``` ruby
3939threads = Array .new (3 ) { |i | Thread .new { ch.pop } }
4040sleep 0.01 # let the threads run
41- threads
42- # => [#<Thread:0x000006@channel.in.md:32 dead>,
43- # #<Thread:0x000007@channel.in.md:32 dead>,
44- # #<Thread:0x000008@channel.in.md:32 sleep_forever>]
41+ threads.map(& :status ) # => [false, false, "sleep"]
4542ch.push message: 3
4643# => #<Concurrent::Promises::Channel:0x000002 capacity taken 0 of 2>
4744threads.map(& :value )
48- # => [{:message=>1 }, {:message=>2 }, {:message=>3}]
45+ # => [{:message=>0 }, {:message=>1 }, {:message=>3}]
4946```
5047
5148### Promises integration
@@ -55,11 +52,11 @@ therefore all operations can be represented as futures.
5552
5653``` ruby
5754ch = Concurrent ::Promises ::Channel .new 2
58- # => #<Concurrent::Promises::Channel:0x000009 capacity taken 0 of 2>
55+ # => #<Concurrent::Promises::Channel:0x000006 capacity taken 0 of 2>
5956push_operations = Array .new (3 ) { |i | ch.push_op message: i }
60- # => [#<Concurrent::Promises::Future:0x00000a fulfilled with #<Concurrent::Promises::Channel:0x000009 capacity taken 2 of 2>>,
61- # #<Concurrent::Promises::Future:0x00000b fulfilled with #<Concurrent::Promises::Channel:0x000009 capacity taken 2 of 2>>,
62- # #<Concurrent::Promises::ResolvableFuture:0x00000c pending>]
57+ # => [#<Concurrent::Promises::Future:0x000007 fulfilled with #<Concurrent::Promises::Channel:0x000006 capacity taken 2 of 2>>,
58+ # #<Concurrent::Promises::Future:0x000008 fulfilled with #<Concurrent::Promises::Channel:0x000006 capacity taken 2 of 2>>,
59+ # #<Concurrent::Promises::ResolvableFuture:0x000009 pending>]
6360```
6461
6562> We do not have to sleep here letting the futures execute as Threads.
@@ -73,14 +70,14 @@ making a space for a new message.
7370``` ruby
7471ch.pop_op.value! # => {:message=>0}
7572push_operations.map(& :value! )
76- # => [#<Concurrent::Promises::Channel:0x000009 capacity taken 2 of 2>,
77- # #<Concurrent::Promises::Channel:0x000009 capacity taken 2 of 2>,
78- # #<Concurrent::Promises::Channel:0x000009 capacity taken 2 of 2>]
73+ # => [#<Concurrent::Promises::Channel:0x000006 capacity taken 2 of 2>,
74+ # #<Concurrent::Promises::Channel:0x000006 capacity taken 2 of 2>,
75+ # #<Concurrent::Promises::Channel:0x000006 capacity taken 2 of 2>]
7976
8077pop_operations = Array .new (3 ) { |i | ch.pop_op }
81- # => [#<Concurrent::Promises::ResolvableFuture:0x00000d fulfilled with {:message=>1}>,
82- # #<Concurrent::Promises::ResolvableFuture:0x00000e fulfilled with {:message=>2}>,
83- # #<Concurrent::Promises::ResolvableFuture:0x00000f pending>]
78+ # => [#<Concurrent::Promises::ResolvableFuture:0x00000a fulfilled with {:message=>1}>,
79+ # #<Concurrent::Promises::ResolvableFuture:0x00000b fulfilled with {:message=>2}>,
80+ # #<Concurrent::Promises::ResolvableFuture:0x00000c pending>]
8481ch.push message: 3 # (push|pop) can be freely mixed with (push_o|pop_op)
8582pop_operations.map(& :value )
8683# => [{:message=>1}, {:message=>2}, {:message=>3}]
@@ -94,21 +91,21 @@ returns a pair to be able to find out which channel had the message available.
9491
9592``` ruby
9693ch1 = Concurrent ::Promises ::Channel .new 2
97- # => #<Concurrent::Promises::Channel:0x000010 capacity taken 0 of 2>
94+ # => #<Concurrent::Promises::Channel:0x00000d capacity taken 0 of 2>
9895ch2 = Concurrent ::Promises ::Channel .new 2
99- # => #<Concurrent::Promises::Channel:0x000011 capacity taken 0 of 2>
96+ # => #<Concurrent::Promises::Channel:0x00000e capacity taken 0 of 2>
10097ch1.push 1
101- # => #<Concurrent::Promises::Channel:0x000010 capacity taken 1 of 2>
98+ # => #<Concurrent::Promises::Channel:0x00000d capacity taken 1 of 2>
10299ch2.push 2
103- # => #<Concurrent::Promises::Channel:0x000011 capacity taken 1 of 2>
100+ # => #<Concurrent::Promises::Channel:0x00000e capacity taken 1 of 2>
104101
105102Concurrent ::Promises ::Channel .select ([ch1, ch2])
106- # => [#<Concurrent::Promises::Channel:0x000010 capacity taken 0 of 2>, 1]
103+ # => [#<Concurrent::Promises::Channel:0x00000d capacity taken 0 of 2>, 1]
107104ch1.select (ch2)
108- # => [#<Concurrent::Promises::Channel:0x000011 capacity taken 0 of 2>, 2]
105+ # => [#<Concurrent::Promises::Channel:0x00000e capacity taken 0 of 2>, 2]
109106
110107Concurrent ::Promises .future { 3 + 4 }.then_channel_push(ch1)
111- # => #<Concurrent::Promises::Future:0x000012 pending>
108+ # => #<Concurrent::Promises::Future:0x00000f pending>
112109Concurrent ::Promises ::Channel .
113110 # or `ch1.select_op(ch2)` would be equivalent
114111 select_op([ch1, ch2]).
@@ -125,7 +122,7 @@ They always return immediately and indicate either success or failure.
125122
126123``` ruby
127124ch
128- # => #<Concurrent::Promises::Channel:0x000009 capacity taken 0 of 2>
125+ # => #<Concurrent::Promises::Channel:0x000006 capacity taken 0 of 2>
129126ch.try_push 1 # => true
130127ch.try_push 2 # => true
131128ch.try_push 3 # => false
@@ -142,7 +139,7 @@ when the timeout option is used.
142139
143140``` ruby
144141ch
145- # => #<Concurrent::Promises::Channel:0x000009 capacity taken 0 of 2>
142+ # => #<Concurrent::Promises::Channel:0x000006 capacity taken 0 of 2>
146143ch.push 1 , 0.01 # => true
147144ch.push 2 , 0.01 # => true
148145ch.push 3 , 0.01 # => false
@@ -159,7 +156,7 @@ if the consumers are not keeping up.
159156
160157``` ruby
161158channel = Concurrent ::Promises ::Channel .new 2
162- # => #<Concurrent::Promises::Channel:0x000013 capacity taken 0 of 2>
159+ # => #<Concurrent::Promises::Channel:0x000010 capacity taken 0 of 2>
163160log = Concurrent ::Array .new # => []
164161
165162producers = Array .new 2 do |i |
@@ -170,8 +167,8 @@ producers = Array.new 2 do |i|
170167 end
171168 end
172169end
173- # => [#<Thread:0x000014 @channel.in.md:133 run>,
174- # #<Thread:0x000015 @channel.in.md:133 run>]
170+ # => [#<Thread:0x000011 @channel.in.md:133 run>,
171+ # #<Thread:0x000012 @channel.in.md:133 run>]
175172
176173consumers = Array .new 4 do |i |
177174 Thread .new (i) do |consumer |
@@ -183,22 +180,22 @@ consumers = Array.new 4 do |i|
183180 end
184181 end
185182end
186- # => [#<Thread:0x000016 @channel.in.md:142 run>,
187- # #<Thread:0x000017 @channel.in.md:142 run>,
188- # #<Thread:0x000018 @channel.in.md:142 run>,
189- # #<Thread:0x000019 @channel.in.md:142 run>]
183+ # => [#<Thread:0x000013 @channel.in.md:142 run>,
184+ # #<Thread:0x000014 @channel.in.md:142 run>,
185+ # #<Thread:0x000015 @channel.in.md:142 run>,
186+ # #<Thread:0x000016 @channel.in.md:142 run>]
190187
191188# wait for all to finish
192189producers.map(& :join )
193- # => [#<Thread:0x000014 @channel.in.md:133 dead>,
194- # #<Thread:0x000015 @channel.in.md:133 dead>]
190+ # => [#<Thread:0x000011 @channel.in.md:133 dead>,
191+ # #<Thread:0x000012 @channel.in.md:133 dead>]
195192consumers.map(& :join )
196- # => [#<Thread:0x000016 @channel.in.md:142 dead>,
197- # #<Thread:0x000017 @channel.in.md:142 dead>,
198- # #<Thread:0x000018 @channel.in.md:142 dead>,
199- # #<Thread:0x000019 @channel.in.md:142 dead>]
193+ # => [#<Thread:0x000013 @channel.in.md:142 dead>,
194+ # #<Thread:0x000014 @channel.in.md:142 dead>,
195+ # #<Thread:0x000015 @channel.in.md:142 dead>,
196+ # #<Thread:0x000016 @channel.in.md:142 dead>]
200197# investigate log
201- log
198+ log
202199# => ["producer 0 pushing 0",
203200# "producer 0 pushing 1",
204201# "producer 0 pushing 2",
@@ -229,7 +226,7 @@ that run a thread pool.
229226
230227``` ruby
231228channel = Concurrent ::Promises ::Channel .new 2
232- # => #<Concurrent::Promises::Channel:0x00001a capacity taken 0 of 2>
229+ # => #<Concurrent::Promises::Channel:0x000017 capacity taken 0 of 2>
233230log = Concurrent ::Array .new # => []
234231
235232def produce (channel , log , producer , i )
@@ -251,22 +248,22 @@ end # => :consume
251248producers = Array .new 2 do |i |
252249 Concurrent ::Promises .future(channel, log, i) { |* args | produce * args, 0 }.run
253250end
254- # => [#<Concurrent::Promises::Future:0x00001b pending>,
255- # #<Concurrent::Promises::Future:0x00001c pending>]
251+ # => [#<Concurrent::Promises::Future:0x000018 pending>,
252+ # #<Concurrent::Promises::Future:0x000019 pending>]
256253
257254consumers = Array .new 4 do |i |
258255 Concurrent ::Promises .future(channel, log, i) { |* args | consume * args, 0 }.run
259256end
260- # => [#<Concurrent::Promises::Future:0x00001d pending>,
261- # #<Concurrent::Promises::Future:0x00001e pending>,
262- # #<Concurrent::Promises::Future:0x00001f pending>,
263- # #<Concurrent::Promises::Future:0x000020 pending>]
257+ # => [#<Concurrent::Promises::Future:0x00001a pending>,
258+ # #<Concurrent::Promises::Future:0x00001b pending>,
259+ # #<Concurrent::Promises::Future:0x00001c pending>,
260+ # #<Concurrent::Promises::Future:0x00001d pending>]
264261
265262# wait for all to finish
266263producers.map(& :value! ) # => [:done, :done]
267264consumers.map(& :value! ) # => [:done, :done, :done, :done]
268265# investigate log
269- log
266+ log
270267# => ["producer 0 pushing 0",
271268# "producer 1 pushing 0",
272269# "consumer 1 got 0. payload 0 from producer 1",
@@ -295,19 +292,19 @@ The operations have to be paired to succeed.
295292
296293``` ruby
297294channel = Concurrent ::Promises ::Channel .new 0
298- # => #<Concurrent::Promises::Channel:0x000021 capacity taken 0 of 0>
295+ # => #<Concurrent::Promises::Channel:0x00001e capacity taken 0 of 0>
299296thread = Thread .new { channel.pop }; sleep 0.01
300297# allow the thread to go to sleep
301298thread
302- # => #<Thread:0x000022 @channel.in.md:214 sleep_forever>
299+ # => #<Thread:0x00001f @channel.in.md:246 sleep_forever>
303300# succeeds because there is matching pop operation waiting in the thread
304301channel.try_push(:v1 ) # => true
305302# remains pending, since there is no matching operation
306303push = channel.push_op(:v2 )
307- # => #<Concurrent::Promises::ResolvableFuture:0x000023 pending>
304+ # => #<Concurrent::Promises::ResolvableFuture:0x000020 pending>
308305thread.value # => :v1
309306# the push operation resolves as a pairing pop is called
310307channel.pop # => :v2
311308push
312- # => #<Concurrent::Promises::ResolvableFuture:0x000023 fulfilled with #<Concurrent::Promises::Channel:0x000021 capacity taken 0 of 0>>
309+ # => #<Concurrent::Promises::ResolvableFuture:0x000020 fulfilled with #<Concurrent::Promises::Channel:0x00001e capacity taken 0 of 0>>
313310```
0 commit comments