Threads are the basis of multi-tasking in a program. Without them everything will have to be done serially-- now imagine how clumsily would our internet browsers, games, and all the applications we use daily would behave if that were so! Thankfully they are out there as our obedient workers running number of different tasks simultaneously.
Today, we shall see how to use threads in Perl programming language. Also, we will learn how to share variables among threads (like friends share cookies). Let's take a very very simple example:
#!/usr/bin/perl
use threads;
use threads::shared;
our $variable1 :shared;
our $variable2 :shared;
my $thread1 = threads->new(\&function1, "function1");
my $thread2 = threads->new(\&function2, "function2");
$thread1->join();
$thread2->join();
sub thread1()
{
print "hello i am thread1 :)\n";
print "value of variable1: $variable1 and variable2: $variable2\n";
variable1 = 1;
variable2 = 2;
}
sub thread2()
{
print "hello i am thread2 :)\n";
print "value of variable1: $variable1 and variable2: $variable2\n";
variable1 = 3;
variable2 = 4;
}
See! making threads in Perl is that easy. Just declare to SUB's and assign them each to a thread. Then call join() function for each thread you declared and they will get going.
There are two things to note here:
Today, we shall see how to use threads in Perl programming language. Also, we will learn how to share variables among threads (like friends share cookies). Let's take a very very simple example:
#!/usr/bin/perl
use threads;
use threads::shared;
our $variable1 :shared;
our $variable2 :shared;
my $thread1 = threads->new(\&function1, "function1");
my $thread2 = threads->new(\&function2, "function2");
$thread1->join();
$thread2->join();
sub thread1()
{
print "hello i am thread1 :)\n";
print "value of variable1: $variable1 and variable2: $variable2\n";
variable1 = 1;
variable2 = 2;
}
sub thread2()
{
print "hello i am thread2 :)\n";
print "value of variable1: $variable1 and variable2: $variable2\n";
variable1 = 3;
variable2 = 4;
}
See! making threads in Perl is that easy. Just declare to SUB's and assign them each to a thread. Then call join() function for each thread you declared and they will get going.
There are two things to note here:
- When a thread is created and is running the assigned sub, it receives a new copy of any variable you use in it. This means that any variables declared outside the subroutine are not accessible in it. So a variable being used in another thread is practically invisible to it! Now, we want to share our variable1 and variable2 between the two threads. So we use threads and threads::shared packages and put a little ":shared" thing after our global variables (pun IS intended here :D). Now our threads can modify these variables and read modifications done by each other too!
- The threads package used here can be installed from CPAN using the command cpan install threads on all compatible platforms like Linux (for Linux you may have to use sudo cpan install threads), Windows or Mac.
0 comments:
Post a Comment