Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update example code for ::spawn with WaitGroup #15191

Merged
merged 2 commits into from
Nov 15, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"
BigBoyBarney marked this conversation as resolved.
Show resolved Hide resolved
#
# 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
Loading