Reader small image

You're reading from  Perl 6 Deep Dive

Product typeBook
Published inSep 2017
Reading LevelIntermediate
PublisherPackt
ISBN-139781787282049
Edition1st Edition
Languages
Right arrow
Author (1)
Andrew Shitov
Andrew Shitov
author image
Andrew Shitov

Andrew Shitov has been a Perl enthusiast since the end of the 1990s, and is the organizer of over 30 Perl conferences in eight countries. He worked as a developer and CTO in leading web-development companies, such as Art. Lebedev Studio, Booking dotCom, and eBay, and he learned from the "Fathers of the Russian Internet", Artemy Lebedev and Anton Nossik. Andrew has been following the Perl 6 development since its beginning in 2000. He ran a blog dedicated to the language, published a series of articles in the Pragmatic Perl magazine, and gives talks about Perl 6 at various Perl events. In 2017, he published the Perl 6 at a Glance book by DeepText, which was the first book on Perl 6 published after the first stable release of the language specification.
Read more about Andrew Shitov

Right arrow

Concurrent Programming

Perl 6 is a language that was created entirely in the twenty-first century. It is not a surprise that it comes with built-in support of some basic concepts, which makes it easy to create applications that supports parallel and concurrent programming.

In this chapter, we will cover the following topics.

  • Junctions
  • Threads
  • Promises
  • Channels

Junctions

Junctions are one of the simplest examples of where Perl 6 can work in parallel. In the version of Rakudo, which is available at the time of writing this book, this feature is not fully implemented.

A junction is a value that keeps many values at the same time. Examine the following code:

my $j = 1 | 3 | 5;
say 'OK' if $j == 3;
say 'Not OK' if $j != 2;

The variable $j is a junction that keeps three odd numbers, 1, 3, and 5. You may compare $j with an integer and get the Boolean True if the value is one of the values hosted by the junction. In the case of comparing with 3, the result is True , while the second comparison with 2 fails.

Autothreading

Now try passing a junction to a function that takes...

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...

Promises

In the previous section, we were creating some code blocks that were running in parallel. Promises help to see the status of the completeness of such blocks. In Perl 6, promises are handled by the Promise class.

Creating a promise

To create a promise, just call the constructor of the Promise class:

my $promise = Promise.new();

The created object does nothing yet. In the Factory methods section later in this chapter, we will see how to create a promise that executes some code. Meanwhile, let's see the properties that a promise has.

Statuses of a promise

The...

Channels

Channels are the communication mean that can be used for passing data from one piece of code to another. The great thing about channels is that they are thread-compatible, thus it is possible for different threads to talk to each other. In this section, we will learn how to use channels in Perl 6.

Basic use cases

Channels are defined by the Channel class. To create a new channel variable, call the constructor:

my $channel = Channel.new;

Now, we can send data to the channel using the send method and receive it with the receive method:

my $channel = Channel.new;
$channel.send(42);

my $value = $channel.receive();
say $value;

This program does a trivial thing—it sends the value to the channel and reads it immediately...

Summary

In this chapter, we briefly talked about the parallel nature of junctions, and spent the rest of the time deeply examining threads, promises, and channels—the mechanism that implement parallel and concurrent features in Perl 6. Using them is quite easy for the developer and does not require any manual manipulation of underlying mechanisms for ensuring that threads are executing without collisions.

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Perl 6 Deep Dive
Published in: Sep 2017Publisher: PacktISBN-13: 9781787282049
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Andrew Shitov

Andrew Shitov has been a Perl enthusiast since the end of the 1990s, and is the organizer of over 30 Perl conferences in eight countries. He worked as a developer and CTO in leading web-development companies, such as Art. Lebedev Studio, Booking dotCom, and eBay, and he learned from the "Fathers of the Russian Internet", Artemy Lebedev and Anton Nossik. Andrew has been following the Perl 6 development since its beginning in 2000. He ran a blog dedicated to the language, published a series of articles in the Pragmatic Perl magazine, and gives talks about Perl 6 at various Perl events. In 2017, he published the Perl 6 at a Glance book by DeepText, which was the first book on Perl 6 published after the first stable release of the language specification.
Read more about Andrew Shitov