Oct 202013
 
Article Perl

A computer program can execute several tasks simultaneously using the technique known as “multithreading”. With multithreading, a thread can be executing a CPU intensive process, while other is executing a disk I/O operation. A third thread is running a query against a database, that might reside in a different computer, and a fourth thread may be busy downloading content from the network. In this way, the usage of resources available is optimized, and the total execution time is reduced.

This post reviews the basic concepts of multithreading, with some sample perl scripts that implement this technique.

Support for multithreading in the perl interpreter

From version 5.8. multithreading is supported by the perl interpreter, if it has been compiled with this option.

A simple script can be used to check that the perl interpreter in our system has support for multithreading:

The execution of this script determines if our system has support for multithreading. If it does not, we need to install a different version of the interpreter, or compile the interpreter from source, adding the relevant option.

In our case, running the script above shows that both our Debian server (running perl 5.12) and our Windows 7 desktop (with ActivePerl 5.12) include support for multithreading.

Executing a second thread

The thread that executes when the program starts is called the ‘main’ thread. This sample code starts the execution of a second thread, while the main thread continues executing:

As shown in the example, thread management is done by means of the ‘threads’ package. The second thread is created in a call to the ‘create’ method (it is also possible to use the “new()” method, it is just a synonym of “create()”). The create() method receives as argument a reference to the “sub1()” subroutine. The second thread executes the code in this subroutine.

Finally, before the program finishes execution, the main thread must make a call to the “join()” method. With this call, is keeps waiting until the second thread stops executing.

Optionally, the second thread can return information to the main thread. In the example above, the return value is a single text string. The return value could also be an array. In this case, the thread must be created in “list mode”, enclosing the $thr variable in parentheses:

Detach the secondary thread

If there is no need for the secondary thread to return a value to the main thread, the “detach()” method can be called after the thread has been launched. Then, the main thread does not have to wait for the secondary thread:

A thread can also detach itself from the main thread, calling the “detach()” method as a static method of the “threads” class:

Shared variables

At the time a secondary thread is created, a copy of all the variables in the creating thread is made available to the new thread. Modifications made by the secondary thread to these variables are now visible to the creating thread, and viceversa.

Shared variables can be defined, if the sentence  “use threads::shared” is added. Next, variables defined with the modifier “:shared” are shared between threads:

The main thread in the sample code prints the value “2”, after the variable has been incremented in the secondary thread.

It is also possible to share arrays and hastables with the “:shared” modifier. But, if the values in those data structures are references to other variables, the variables referenced must also be shared variables.

Index of posts related to Perl programming

 Posted by at 3:52 pm

 Leave a Reply

(required)

(required)