File tree Expand file tree Collapse file tree 1 file changed +17
-10
lines changed Expand file tree Collapse file tree 1 file changed +17
-10
lines changed Original file line number Diff line number Diff line change @@ -59,16 +59,23 @@ C<hyper> and C<race> use (maybe simultaneous) threads to run different
59
59
iterations in a loop:
60
60
61
61
= 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.
72
79
73
80
= end pod
74
81
You can’t perform that action at this time.
0 commit comments