Before reading further... Are you looking for great Linux hosting from a company that cares about GNU/Linux? Pick Dreamhost hosting, get a 10% bonus to the disk space (and support Free Software Magazine in the meantime!)
A chat project: back from the unknown
Perl threads, open standards and interoperability. An explorer’s report
Download the whole article as PDF
- 2005-08-04
- Server side | Intermediate
-
Write a full post in response to this!
Some weeks ago I (Marco) was looking for new things to learn in Perl. I took a look at my library and reviewed the titles of the books I read less, and after some consideration found two topics: GUIs and threads. But since I hate the “hello world” kind of programs, I decided to start this exploration of the (for me) unknown parts of Perl with a somewhat meaningful application: a chat.
The chat project
When I do this sort of thing, I like to discuss my ideas with other people, and that’s where Stefano always comes in. Stefano is a Java programmer, and knows very well how to deal with GUIs and threads. We talked about the project, and he gave me some structural advice for the program. Besides, we thought, doing things with a standard protocol like HTTP would greatly simplify the design, since there were a lot of ready-to-use modules on the CPAN to handle it, and it would help to make the application interoperable.
Essentially, I had two sources from which to study Perl threads: the chapter about them in the “Camel Book”, that is “Programming Perl” by Wall, Christiansen and Orwant; and the “perlthrtut” document, which is bundled with all recent Perl distributions. Other information was spread across the documentation of the threads, threads::shared and Thread::Queue modules.
It’s a peer-to-peer application: you run it specifying the address and port you want to contact another instance of the application, and you are given a GUI where you can type text and read replies
After experimenting a bit with the new things that I was learning, I decided that it was time to go for the real thing. I then picked up the “Learning Perl/tk” book, which I had read a while ago but never applied in practice, and reviewed those parts that allowed me to create a simple interface: a window with a big textbox to display the conversation in, a text field for the input, and a button to submit the typed text to the chat application.
The design of the application was fairly simple. First, it’s a peer-to-peer application: you run it specifying the address and port you want to contact another instance of the application, and you are given a GUI where you can type text and read replies (a bit like what happens with the old UN*X talk application that you may be familiar with). The communication takes place by means of POST HTTP connections: to send a message to your peer, you submit a POST request to the other peer at the /message URI; the POST request contains two variables: name, set to your nickname, and message, which contains the message to be sent. To receive a message, the application acts as an HTTP server that handles POST requests to the / message URI, formatted as just described above.
The problem with threads
Modern applications perform many operations at the same time. Internet browsers, for example, need to show update information about internal processing while managing multiple connections. Word processors have to assure fast response to user input and, if needed, safely create snapshots of current state for unexpected crashes. Internet browsers and word processors (and multimedia players, games, instant messengers…) are examples of applications that have multithreading capabilities.
Multithreading refers to the ability of an application to divide execution in threads that run simultaneously (or, at least, they appear to). While in the multiprocess model each process runs in its own private (and hence safe) environment, threads share the same memory area.
Memory sharing requires from the developer an extra effort to manage concurrent access to the same location. Moreover, a thread may have to wait for another thread to complete before continuing with its execution.
Data structures and classes are called thread-safe when they are able to serialize concurrent access to their internal data. They provide mechanisms to assure that, in a certain time slice, only one thread has read and write access to the shared memory. Vice-versa, non thread-safe structures may have unpredictable, time-dependent behaviour and it’s the programmer’s responsibility to arbitrate data access from concurrent threads. Therefore, depending on the language and library in use, multithreading programming becomes easier or more complex, easy to be adopted or harder to get running. At the same time, libraries can be totally or partially thread-safe, leaving it up to the developer to write their own safe structures.
Depending on the language and library in use, multithreading programming becomes easier or more complex, easy to be adopted or harder to get running
The multithreading approach simplifies the design and implementation of applications in all those cases where they can be described as a set of co-ordinated, parallel tasks. There are even cases when programming in a non-multithreaded model is not an option. In the Java2 Micro Edition runtime, for example, you are required to manage network connections using a separate thread for communication routines.
The problem with Perl threads
Besides all of the caveats that are typical of threads programming, Perl adds some more that are specific to the language. For example, in recent versions of Perl, no data is shared by default between threads, which means that you must specify what you want to share explicitly. Besides, if you want to share objects then you know that you are going to run into trouble, and if you want to share objects that contain globs in their data structures (that means, for example, objects that contain filehandles and sockets) you are going to run into big trouble! This forces everybody that is going to design a threaded Perl application using objects to be very careful, or to use one of the alternatives around, like POE, and give up with Perl threads.
Oh and instead, a Thread::Queue object can be shared between threads, but no: you can’t share objects using it!
Write a full post in response to this!
Download the files attached with this article.
Similar articles
Do you like this post?
Vote for it!
Copyright information
This article is made available under the "Attribution-NonCommercial" Creative Commons License 3.0 available from http://creativecommons.org/licenses/by-nc/3.0/.
Biography
Marco Marongiu: Born in 1971, Marongiu graduated in applied mathematics in 1997; he's now a full-time system administrator for a European ISP. He's also a Perl programmer and technical writer and lecturer by passion, and is interested in web and XML related technologies. Marongiu has been a Debian User since version 1.1.10 and he helped found the GULCh Linux Users Group (Gruppo Utenti Linux Cagliari), the first one in Sardinia. He recently became a father to his first son Andrea, and he's been trying to reorganise his life since, so that he can start writing technical articles again and holding seminars.
Stefano Sanna: Stefano Sanna is a Researcher and Software Engineer in the Network Distributed Applications group of CRS4 (Centre for Advanced Studies, Research and Development in Sardinia). He is specialized in application design and development for J2ME and PersonalJava platforms on PDAs and mobile phone. He was also co-founder of the GULCh LUG.
- Login or register to post comments
- 9265 reads
- Printer friendly version (unavailable!)




Similar entries
Buzz authors
All news
Other sites
- The Top 10 Everything (Dave). The good, the bad and the ugly.
- Free Software news (Dave & Bridget). All about free software -- free as in freedom!
- Book Reviews: Illiterarty (Bridget). Book reviews, blogs, and short stories.
Hot topics - last 21 days
-
Unjustifiable Criticism of Richard Stallman by Linus Torvalds
Paul Gaskin, 2008-11-17 -
Mixing free and proprietary software: not a rosy future
Ryan Cartwright, 2008-11-13 -
How to help build a free software search engine and use it too. Welcome to Wikia
Gary Richmond, 2008-11-19 -
Freedom is an enabler, not a feature
bogdanbiv, 2008-11-15
Hot topics - last 60 days
-
Why is The Bizarre Cathedral licence "non-free"?
Ryan Cartwright, 2008-10-21 -
Ten easy ways to attract women to your free software project
Terry Hancock, 2008-09-22 -
Unjustifiable Criticism of Richard Stallman by Linus Torvalds
Paul Gaskin, 2008-11-17 -
Fighting the "legacy" reputations of GNU/Linux, seventeen years later
Ryan Cartwright, 2008-10-13 -
Becoming a free software developer, part V: When and where did you learn?
Rosalyn Hunter, 2008-09-24

Dedicated server
Did you consider a more native solution, as in POE?
Submitted by admin on Thu, 2006-03-30 05:36.
Vote!From: Randal L. Schwartz
Url: http://www.stonehenge.com/merlyn/
Date: 2005-08-08
Subject: Did you consider a more native solution, as in POE?
I've done projects with POE and Tk before (http://www.stonehenge.com/merlyn/PerlJournal/col11.html), and found the result to be quite satisfactory. In Unix thread is spelled "f o r k", so the "thread" support in Perl is really mostly for Windows people to be happy. POE works nicely on both Windows and Unix though.