Concurrent computing: Difference between revisions

→‎{{header|D}}: add crystal implementation with fibers and channels
(Corrected syntax to compile with Nim 1.4)
(→‎{{header|D}}: add crystal implementation with fibers and channels)
Line 394:
(bordeaux-threads:make-thread (writer "Rosetta"))
(bordeaux-threads:make-thread (writer "Code")))))</lang>
 
=={{header|Crystal}}==
Crystal requires the use of channels to ensure that the main fiber doesn't exit before any of the new fibers are done, since each fiber sleeping could return control to the main fiber.
<lang ruby>require "channel"
require "fiber"
require "random"
 
done = Channel(Nil).new
 
"Enjoy Rosetta Code".split.map do |x|
spawn do
sleep Random.new.rand(0..500).milliseconds
puts x
done.send nil
end
end
 
3.times do
done.receive
end</lang>
 
=={{header|D}}==
Anonymous user