Skip to content

Commit

Permalink
Update example code for ::spawn with WaitGroup (#15191)
Browse files Browse the repository at this point in the history
Co-authored-by: Sijawusz Pur Rahnama <sija@sija.pl>
  • Loading branch information
BigBoyBarney and Sija authored Nov 15, 2024
1 parent f4606b7 commit 32be3b6
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions src/concurrent.cr
Original file line number Diff line number Diff line change
Expand Up @@ -33,31 +33,35 @@ end

# Spawns a new fiber.
#
# The newly created fiber doesn't run as soon as spawned.
# NOTE: The newly created fiber doesn't run as soon as spawned.
#
# Example:
# ```
# # Write "1" every 1 second and "2" every 2 seconds for 6 seconds.
#
# ch = Channel(Nil).new
# require "wait_group"
#
# wg = WaitGroup.new 2
#
# spawn do
# 6.times do
# sleep 1.second
# puts 1
# end
# ch.send(nil)
# ensure
# wg.done
# end
#
# spawn do
# 3.times do
# sleep 2.seconds
# puts 2
# end
# ch.send(nil)
# ensure
# wg.done
# end
#
# 2.times { ch.receive }
# wg.wait
# ```
def spawn(*, name : String? = nil, same_thread = false, &block)
fiber = Fiber.new(name, &block)
Expand Down

0 comments on commit 32be3b6

Please sign in to comment.