ithreads are a new thread API in the perl programming language. Prior to perl 5.8.0, threaded programming could be implemented by using the module. Interpreter threads, or "ithreads" are based somewhat on this old model known as "5005 threads", but the two differ in more than a few ways. For those of you used to threaded programming other in languages, ithreads will be quite a big change.

To use ithreads, first you must obtain a copy of perl 5.8.0 or higher. Download a copy from the CPAN( if you haven't already. If you have a version of perl higher than 5.8.0 installed, you may have to install a new copy if you didn't explicitly ask for ithreads when you compiled it. You must pass the -Dusethreads argument to the Configure script at the beginning of the installation process or ithreads will not be included. Also, it is important to note that ithreads will slow perl down, even if you're not using the module in your scripts. Because of this, you might not want to replace your existing perl interpreter with an ithreads enabled version. Installing the ithreads enabled version somewhere such as your home directory is probably a better idea.

Once perl is properly installed, you can start using ithreads. To do so, include the "use threads;" statement in your program. Before you get started with coding, though, there are a few other things you should know. Most importantly, you should be aware that ithreads do not share data between threads. This is where ithreads and many other thread models differ. Newly created threads recieve copies of any global variables declared in the main thread and any changes made will be discared once the thread returns, much like fork(). Thankfully, the threads::shared module is available which allows shared data to exists between threads, however there are a few restrictions that come along with it, but that's for another writeup. Be sure to read the man page for threads::shared thoroughly if you decide to use it.

Most of the methods found in the old implementation are still available ithreads, but for those of you new threaded programming in perl, here's a brief description of the main functions you need to know. Consult perldoc or the man pages for more detailed documentation.

threads->create("function name", ARGS)
This will create a new thread of execution, beginning in the "function name" subroutine. Any arguments you wish to pass can be placed in ARGS. The return value is a new 'threads' object which you can use to manipulate the thread with the following methods:

This method is to be called on a previously create()'d thread object, in this case, $t. join() will wait for the thread in question to finish and deliver any return values the function had to offer.

Call this method if you don't care if or when the thread exits and you don't want any return values. If you have already detach()'d a thread, you cannot join() it.

Use this function whenever you feel like being nice and giving some CPU time to other threads currently executing.

There are a few others that you should be aware of as well, but for the sake of brevity I won't include them here. Check the man pages for details.

Overall, I have to say that using ithreads has been a strange experience. I've recieved penty of 'segmentation fault' messages at the end of my scripts, which is certainly a rare thing for me to see(as far as perl goes anyway). The lack of shared memory annoys me, and since threads::semaphore isn't exactly friendly with complex data structures, it makes a lot of non trivial applications more difficult to implement. I'm hoping that perl 6 will rework a few things and produce a thread model that will be accepted by the masses. Until then, python might be a good alternative.

the 'threads', 'threads::shared', and 'perlthrtut' man pages

Log in or register to write something here or to contact authors.