Browse files

+ Best case output preserved

  • Loading branch information...
1 parent a44d7a7 commit 7f24ea57ca57e3fde740d1e2efe6c6c5294fe751 @kschiess committed Mar 30, 2012
Showing with 35 additions and 10 deletions.
  1. +35 −10 website/source/tutorial/pipes.html.textile
View
45 website/source/tutorial/pipes.html.textile
@@ -135,24 +135,49 @@ for that as well. Here's an example of N message producers, one consumer:
And here 's an example of one producer, M consumers:
-<pre class="sh_ruby"><code title="m consumers">
- pipe = Cod.pipe
-
- pids = 10.times.map do
- fork do
- work_item_no = pipe.get
- puts "#{Process.pid} received #{work_item_no}."
+<pre class="sh_ruby"><code title="m consumers">
+ Worker = Struct.new(:n) do
+ def work_on(from, to)
+ loop do
+ work_item = from.get
+ to.put [n, work_item]
+ end
end
end
+ def Worker(n)
+ Worker.new(n)
+ end
+
+ work = Cod.pipe
+ result = Cod.pipe
+
+ pids = 10.times.map do |i|
+ fork { Worker[i].work_on(work, result) }
+ end
+
+ # Distribute some work
+ 10.times { |i| work.put (i+1) }
+
+ # Read back the results
+ 10.times.map {
+ p result.get }
- 10.times { |i| pipe.put (i+1) }
pids.each { |pid| Process.kill(:TERM, pid) }
</code></pre>
-outputs:
+Output will something like this:
<pre class="output">
-3680 received 1.
+[1, 1]
+[3, 2]
+[9, 3]
+[6, 4]
+[4, 5]
+[0, 6]
+[2, 7]
+[2, 8]
+[5, 9]
+[7, 10]
</pre>
h2. The Self Pipe

0 comments on commit 7f24ea5

Please sign in to comment.