PerlSearcher has asked for the wisdom of the Perl Monks concerning the following question:

Hi Perl Monks,

I try to find out what I can use to achieve the below in Perl.
1. Multithreading. I saw the Tread package http://www.perl.com/doc/manual/html/lib/Thread.html. Anything other than this?
2. Communication Among Perl Processes. Anything other than sending signals to process? Can we use Thread package to do this? Any example?

Thanks a lot!
PerlSearcher

------------------------------------------------------------

Update: For multithreading, I found another node on perlmonks at http://www.perlmonks.org/?node_id=615459

  • Comment on Multithreading and Communication Among Perl Processes

Replies are listed 'Best First'.
Re: Multithreading and Communication Among Perl Processes
by Joost (Canon) on Jul 20, 2007 at 23:37 UTC
    The Thread module is deprecated.

    If you want to use threads for anything serious, get a new version of perl (at least 5.8.0, but the latest version, 5.8.8 is probably best) compiled with threads enabled. update: and then get the latest versions of threads and threads::shared from CPAN.

    See also threads and threads::shared.

    As for IPC, threads (not really IPC, but similar), sockets, shared memory and signals are some of the more popular options. Which one is the best/easiest depends on what you're trying to achieve.

    updated again: fixed link to threads::shared

Re: Multithreading and Communication Among Perl Processes
by zentara (Cardinal) on Jul 21, 2007 at 12:10 UTC
    To get you on the right track, super-simple example:
    #!/usr/bin/perl use threads; use threads::shared; use strict; our $a:shared = 1; print "main start->$a\n"; threads->create(\&display); threads->create(\&display1); print "hit enter to finish\n"; <STDIN>; #pauses for keypress print "main done-> $a\n"; sub display { our $a = 2; $a++; print "from t1->$a\n"; } sub display1 { our $a = 99; print "front2->$a\n"; $a++; lock $a; cond_broadcast($a); }

    I'm not really a human, but I play one on earth. Cogito ergo sum a bum
Re: Multithreading and Communication Among Perl Processes
by kingkongrevenge (Scribe) on Jul 21, 2007 at 16:09 UTC
    I think Cache::FileCache is a great simple way to do IPC. It handles locking for you. It is allegedly faster than manually using locks on shared memory or sockets. It's reliably cross platform.

    You effectively get a shared hash table across all your processes. Remember to clear out the cache on startup and shutdown otherwise data will persist between runs.