Skip to content

Commit 1dbaeef

Browse files
committed
Fixes presentation of hyper and race
Thanks to @timo , @jnthn and @lizmat for clarifications.
1 parent de50714 commit 1dbaeef

File tree

1 file changed

+17
-10
lines changed

1 file changed

+17
-10
lines changed

doc/Language/statement-prefixes.pod6

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -59,16 +59,23 @@ C<hyper> and C<race> use (maybe simultaneous) threads to run different
5959
iterations in a loop:
6060
6161
=for code
62-
my $main-thread = $*THREAD.id;
63-
my $saw-another-thread = False;
64-
race for ^10000 {
65-
$saw-another-thread = True if $*THREAD.id != $main-thread;
66-
}
67-
ok $saw-another-thread, 'A race for loop runs the body over threads';
68-
69-
Unless you can come up with a real use case it's probably better that you try
70-
and reduce your problem to a single data structure and use them in L<method
71-
form|/routine/hyper>.
62+
my @a = hyper for ^100_000 { .is-prime }
63+
64+
This code is around 3x faster than the bare for. But there are a couple of
65+
caveats here:
66+
67+
=item The operation inside the loop should take enough time to make threading
68+
make sense.
69+
70+
=item There should be no read or write access to the same data structure inside
71+
the loop. Let the loop produce a result, and assign it.
72+
73+
=item If there's I/O operation inside the loop, there might be some contention
74+
so please avoid it.
75+
76+
Main difference between C<hyper> and C<race> is the ordering of results. Use
77+
C<hyper> if you need the loop results to be produced in order, C<race> if you
78+
don't care.
7279
7380
=end pod
7481

0 commit comments

Comments
 (0)