Skip to content
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
40 changes: 40 additions & 0 deletions guides/asynchronous-tasks/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,46 @@ Async do
end
```

### Stopping all Tasks held in a Barrier

To stop (terminate/cancel) the all tasks held in a barrier:

```ruby
barrier = Async::Barrier.new

Async do
tasks = 3.times.map do |i|
barrier.async do
sleep 1
puts "Hello World #{i}"
end
end

barrier.stop
end
```

If you're letting individual tasks held by a barrier throw unhandled exceptions, be sure to call ({ruby Async::Barrier#stop}):

```ruby
barrier = Async::Barrier.new

Async do
tasks = 3.times.map do |i|
barrier.async do
sleep 1
puts "Hello World #{i}"
end
end

begin
barrier.wait
ensure
barrier.stop
end
end
```

## Resource Management

In order to ensure your resources are cleaned up correctly, make sure you wrap resources appropriately, e.g.:
Expand Down