Sha256: 3cc8eba6ef001598c5597e9566b6dbac9b72c2a95090bb233f3e8c91319afce3

Contents?: true

Size: 1.02 KB

Versions: 18

Compression:

Stored size: 1.02 KB

Contents

A synchronization primitive, which allows you to wait for tasks to complete in order of completion. This is useful for implementing a task pool, where you want to wait for the first task to complete, and then cancel the rest.

If you try to wait for more things than you have added, you will deadlock.

## Example

~~~ ruby
require 'async'
require 'async/semaphore'
require 'async/barrier'
require 'async/waiter'

Sync do
	barrier = Async::Barrier.new
	waiter = Async::Waiter.new(parent: barrier)
	semaphore = Async::Semaphore.new(2, parent: waiter)
	
	# Sleep sort the numbers:
	generator = Async do
		while true
			semaphore.async do |task|
				number = rand(1..10)
				sleep(number)
			end
		end
	end
	
	numbers = []
	
	4.times do
		# Wait for all the numbers to be sorted:
		numbers << waiter.wait
	end
	
	# Don't generate any more numbers:
	generator.stop
	
	# Stop all tasks which we don't care about:
	barrier.stop
	
	Console.info("Smallest", numbers)
end
~~~

### Output

~~~
0.0s     info: Smallest
             | [3, 3, 1, 2]
~~~

Version data entries

18 entries across 18 versions & 1 rubygems

Version Path
async-2.21.0 lib/async/waiter.md
async-2.20.0 lib/async/waiter.md
async-2.19.0 lib/async/waiter.md
async-2.18.0 lib/async/waiter.md
async-2.17.0 lib/async/waiter.md
async-2.16.1 lib/async/waiter.md
async-2.16.0 lib/async/waiter.md
async-2.15.3 lib/async/waiter.md
async-2.15.2 lib/async/waiter.md
async-2.15.1 lib/async/waiter.md
async-2.15.0 lib/async/waiter.md
async-2.14.2 lib/async/waiter.md
async-2.14.1 lib/async/waiter.md
async-2.14.0 lib/async/waiter.md
async-2.13.0 lib/async/waiter.md
async-2.12.1 lib/async/waiter.md
async-2.12.0 lib/async/waiter.md
async-2.11.0 lib/async/waiter.md