Course of Raku / Functional, concurrent, reactive, and web programming / Concurrent programming / Threads / Exercises / Two threads
Solution: Two threads
Here is a possible solution to the task.
Code
my ($x, $y);
my $a = Thread.start({ $x = 10 * 10 });
my $b = Thread.start({ $y = 20 * 20 });
$a.finish;
$b.finish;
say $x + $y;🦋 You can find the source code in the file two-threads.raku.
Output
500Comments
The two threads run concurrently, each writing its result into its own shared variable.
Reading
$xand$yhappens only after both.finishcalls, so the results are guaranteed to be ready:100 + 400is500. Joining before reading is what makes the value reliable.
Course navigation
← Two threads | Promises →