8000 More documentation. · socketry/async@fa81ca9 · GitHub
[go: up one dir, main page]

Skip to content

Commit fa81ca9

Browse files
committed
More documentation.
1 parent ecb7820 commit fa81ca9

File tree

3 files changed

+55
-5
lines changed

3 files changed

+55
-5
lines changed

lib/async/barrier.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,6 @@ end
3131
### Output
3232

3333
~~~
34-
0.0s info: Sorted [ec=0x104] [pid=50291]
35-
| [0, 0, 0, 0, 1, 2, 2, 3, 6, 6]
34+
0.0s info: Sorted
35+
| [0, 0, 0, 0, 1, 2, 2, 3, 6, 6]
3636
~~~

lib/async/condition.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ end
2525
### Output
2626

2727
~~~
28-
0.0s info: Waiting for condition... [ec=0x3c] [pid=47943]
29-
1.0s info: Signalling condition... [ec=0x64] [pid=47943]
30-
1.0s info: Condition was signalled: Hello World [ec=0x3c] [pid=47943]
28+
0.0s info: Waiting for condition...
29+
1.0s info: Signalling condition...
30+
1.0s info: Condition was signalled: Hello World
3131
~~~

lib/async/waiter.md

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
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.
2+
10000
3+
If you try to wait for more things than you have added, you will deadlock.
4+
5+
## Example
6+
7+
~~~ ruby
8+
require 'async'
9+
require 'async/semaphore'
10+
require 'async/barrier'
11+
require 'async/waiter'
12+
13+
Sync do
14+
barrier = Async::Barrier.new
15+
waiter = Async::Waiter.new(parent: barrier)
16+
semaphore = Async::Semaphore.new(2, parent: waiter)
17+
18+
# Sleep sort the numbers:
19+
generator = Async do
20+
while true
21+
semaphore.async do |task|
22+
number = rand(1..10)
23+
sleep(number)
24+
end
25+
end
26+
end
27+
28+
numbers = []
29+
30+
4.times do
31+
# Wait for all the numbers to be sorted:
32+
numbers << waiter.wait
33+
end
34+
35+
# Don't generate any more numbers:
36+
generator.stop
37+
38+
# Stop all tasks which we don't care about:
39+
barrier.stop
40+
41+
Console.logger.info("Smallest", numbers)
42+
end
43+
~~~
44+
45+
### Output
46+
47+
~~~
48+
0.0s info: Smallest
49+
| [3, 3, 1, 2]
50+
~~~

0 commit comments

Comments
 (0)
0