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

Hello, I tried to write some threaded script and I ran into the warning on the threads module page:
"The use of interpreter-based threads in perl is officially discouraged."
Does this mean we should not try to write multi-threaded scripts in perl ? Use only single-treaded scripts ? That sounds so bad...

Replies are listed 'Best First'.
Re: Can we write multi-threaded scripts ?
by choroba (Archbishop) on Nov 05, 2018 at 09:26 UTC
    See Trying to Understand the Discouragement of Threads or RT: Why are threads discouraged?. "Discouraged" here means threads in Perl are tricky and people in IRC who don't understand them won't help you solving the issues you might encounter. Several monks here have used threads successfully and can probably give you some hints.

    ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,
Re: Can we write multi-threaded scripts ?
by Eily (Monsignor) on Nov 05, 2018 at 09:08 UTC

    There are other ways to have parallel executions in perl, like Parallel::ForkManager.

    That sounds so bad...

    Well, threads are tricky anyway, and for most scripts they would actually make things slower. It's a bad reason for not having parallel execution at all, because it can be useful, but it's often very fine.

Re: Can we write multi-threaded scripts ?
by dasgar (Priest) on Nov 05, 2018 at 17:31 UTC

    Personally, I hate the "discouraged" label being applied to threads - especially considering the official definition of "discouraged" in the perldocs. The wording of that definition leaves a distinct impression that threads are just a step or two away from being removed from Perl, which becomes a heavy handed scare tactic.

    When I first started looking at doing things in parallel, I looked at forks and threads. For some reason, I couldn't get my head wrapped around how to use forks, but I had no trouble with understanding threads. So I started using threads. Some time later, I found documentation (fork in perlport and perlfork) that indicated that Perl will emulate fork using threads in Windows. Since I happen to mainly work in Windows, that means ultimately I will be using threads no matter what I choose between threads or fork.

    In my opinion, using threads in Perl code can be ok. However, you do need to keep some things in mind as you do so. This isn't an exhaustive list, but some items that I can think of are:

    • Not all modules are thread safe. And a large number of modules (possibly most) will not include anything in their documentation to indicate if it is thread safe or not.
    • Portability: If a Perl installation was not compiled to support threads, then your code won't work in that Perl installation.
    • Support: You might have a hard time finding help. And in many forums, you might receive harsh responses about using threads.

    Personally, I'm not going to try to persuade anyone to use or not to use threads in their Perl code. Instead, I would say try to educate yourself so that you can make an informed decision about your choice to use or not to use threads. In my case, I initially made an uninformed decision and later learned enough information to be able to make an informed decision to continue using threads (at least for now).

Re: Can we write multi-threaded scripts ?
by bliako (Prior) on Nov 05, 2018 at 10:20 UTC

    I use threads whenever I think it makes sense and had no problem with them. I use this as a skeleton:

    Beware, shared memory is tricky with them...

Re: Can we write multi-threaded scripts ?
by zentara (Archbishop) on Nov 05, 2018 at 13:16 UTC
    Hi, I've written alot of threaded programs and there are many pitfalls to watch out for. My ultimate observation is that threaded programs are useful only when you need to easily share variables in realtime between threads, using threads::shared. Otherwise, forking solutions using IPC::SysV's shared memory segments, is probably preferrable. Threads just make it easy to share variables, and its tempting to use what is easy.

    Remember, its also officially discouraged to consume alcohol, but how many people do it? :-)

    I'm not really a human, but I play one on earth. ..... an animated JAPH
Re: Can we write multi-threaded scripts ?
by ikegami (Pope) on Nov 06, 2018 at 07:49 UTC

    That warning is poppycock. It should be removed. The developers of Perl explained that it means "The use of interpreter-based threads in perl is officially discouraged if you want a lightweight system for multitasking". In other words, it's completely redundant with the previous paragraph.

    Since creating new threads can be expensive, just use a model that involves reusable worker threads.

Re: Can we write multi-threaded scripts ?
by Anonymous Monk on Nov 05, 2018 at 14:52 UTC
    Check this out:

    MCE - Many-Core Engine for Perl providing parallel processing capabilities

    MCE::Hobo - A threads-like parallelization module

Re: Can we write multi-threaded scripts ?
by jimpudar (Pilgrim) on Nov 05, 2018 at 17:32 UTC

    You may also want to take a look at Mojo::IOLoop::Subprocess, new in Mojolicious 8.0.

    This lets you use normal asynchronous programming style to spawn processes for background work.

    If your work is not CPU bound, you may consider just using asynchronous calls in a single process (see Mojo::IOLoop).



    πάντων χρημάτων μέτρον έστιν άνθρωπος.

Re: Can we write multi-threaded scripts ?
by localshop (Monk) on Nov 05, 2018 at 12:05 UTC
Re: Can we write multi-threaded scripts ?
by Anonymous Monk on Nov 05, 2018 at 15:02 UTC
    These look promising too:

    Forks::Super - extensions and convenience methods to manage background processes

    Parallel::DataPipe - parallel data processing conveyor (links to other options under "See Also")

Re: Can we write multi-threaded scripts ?
by karlgoethebier (Abbot) on Nov 06, 2018 at 22:11 UTC

    Yes we can. But it may take a while. Regards, Karl

    «The Crux of the Biscuit is the Apostrophe»

    perl -MCrypt::CBC -E 'say Crypt::CBC->new(-key=>'kgb',-cipher=>"Blowfish")->decrypt_hex($ENV{KARL});'Help