Threads
In Perl 6, there is the Thread
class, which takes care of creating and running threads. To see in which thread you are at the moment, use the $*THREAD
pseudo constant:
say $*THREAD;
It returns a value of the Thread
class, and the default stringified representation of it is a string containing the identifier and the name of the thread:
Thread #1 (Initial thread)
Don't rely on the particular value of the thread identifier as it may be different even for the main thread.
Starting a thread
In this and in the following sections, we will examine the methods of the Thread
class. We will start, though, with the start
method, which creates a thread and starts its execution.
In the following example, three threads are created. Each of them receives a name and a code block. Code blocks do the same job in each thread and only print the value of the $*THREAD
variable, which will be different within different threads:
say $*THREAD; my $t1 = Thread.start(name => 'Test 1', sub {say $*THREAD}); my $t2...