Threads
Warning! We are delving into the inner depths of Raku now. Threads are a
low-level API and should be avoided by almost all applications. However, if
your particular application needs direct
Thread access, it is here for
you.1
Use of the Thread class in Raku is straight-forward and looks very similar to
what you would expect if you are familiar with threading tools in other
languages:
my $t = Thread.start:
name => 'Background task',
:app_lifetime,
sub {
if rand > 0.5 { say 'weeble' }
else { say 'wobble' }
},
;
say "Starting $t.id(): $t.name()";
say "Main App Thread is $*THREAD.id(): $*THREAD.name()";
$t.finish; # wait for the thread to stop
Give the Thread.start method some code to run and you’re off. The name and
app_lifetime options are optional. If app_lifetime is False (which is the
default), the thread will be terminated when the main application thread
terminates. If set to True, the application will continue to run as long as
this thread is running. Under normal circumstances, only the main thread of your
application has this privilege.