Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7738625
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T08:18:52+00:00 2026-06-01T08:18:52+00:00

If you want to learn how to use Perl interpreter threads, there’s good documentation

  • 0

If you want to learn how to use Perl interpreter threads, there’s good documentation in perlthrtut (threads tutorial) and the threads pragma manpage. It’s definitely good enough to write some simple scripts.

However, I have found little guidance on the web on why and what to sensibly use Perl’s interpreter threads for. In fact, there’s not much talk about them, and if people talk about them it’s quite often to discourage people from using them.

These threads, available when perl -V:useithreads is useithreads='define'; and unleashed by use threads, are also called ithreads, and maybe more appropriately so as they are very different from threads as offered by the Linux or Windows operating systems or the Java VM in that nothing is shared by default and instead a lot of data is copied, not just the thread stack, thus significantly increasing the process size. (To see the effect, load some modules in a test script, then create threads in a loop pausing for key presses each time around, and watch memory rise in task manager or top.)

[…] every time you start a thread all data structures are copied to
the new thread. And when I say all, I mean all. This e.g. includes
package stashes, global variables, lexicals in scope. Everything!

— Things you need to know before programming Perl ithreads (Perlmonks 2003)

When researching the subject of Perl ithreads, you’ll see people discouraging you from using them (“extremely bad idea”, “fundamentally flawed”, or “never use ithreads for anything”).

The Perl thread tutorial highlights that “Perl Threads Are Different”, but it doesn’t much bother to explain how they are different and what that means for the user.

A useful but very brief explanation of what ithreads really are is from the Coro manpage under the heading WINDOWS PROCESS EMULATION. The author of that module (Coro – the only real threads in perl) also discourages using Perl interpreter threads.

Somewhere I read that compiling perl with threads enabled will result in a significantly slower interpreter.

There’s a Perlmonks page from 2003 (Things you need to know before programming Perl ithreads), in which the author asks: “Now you may wonder why Perl ithreads didn’t use fork()? Wouldn’t that have made a lot more sense?” This seems to have been written by the author of the forks pragma. Not sure the info given on that page still holds true in 2012 for newer Perls.

Here are some guidelines for usage of threads in Perl I have distilled from my readings (maybe erroneously so):

  • Consider using non-blocking IO instead of threads, like with HTTP::Async, or AnyEvent::Socket, or Coro::Socket.
  • Consider using Perl interpreter threads on Windows only, not on UNIX because on UNIX, forks are more efficient both for memory and execution speed.
  • Create threads at beginning of program, not when memory concumption already considerable – see “ideal way to reduce these costs” in perlthrtut.
  • Minimize communication between threads because it’s slow (all answers on that page).

So far my research. Now, thanks for any more light you can shed on this issue of threads in Perl. What are some sensible use cases for ithreads in Perl? What is the rationale for using or not using them?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T08:18:54+00:00Added an answer on June 1, 2026 at 8:18 am

    The short answer is that they’re quite heavy (you can’t launch 100+ of them cheaply), and they exhibit unexpected behaviours (somewhat mitigated by recent CPAN modules).

    You can safely use Perl ithreads by treating them as independent Actors.

    1. Create a Thread::Queue::Any for “work”.
    2. Launch multiple ithreads and “result” Queues passing them the (“work” + own “result”) Queues by closure.
    3. Load (require) all the remaining code your application requires (not before threads!)
    4. Add work for the threads into the Queue as required.

    In “worker” ithreads:

    1. Bring in any common code (for any kind of job)
    2. Blocking-dequeue a piece of work from the Queue
    3. Demand-load any other dependencies required for this piece of work.
    4. Do the work.
    5. Pass the result back to the main thread via the “result” queue.
    6. Back to 2.

    If some “worker” threads start to get a little beefy, and you need to limit “worker” threads to some number then launch new ones in their place, then create a “launcher” thread first, whose job it is to launch “worker” threads and hook them up to the main thread.

    What are the main problems with Perl ithreads?

    They’re a little inconvenient with for “shared” data as you need to explicity do the sharing (not a big issue).

    You need to look out for the behaviour of objects with DESTROY methods as they go out of scope in some thread (if they’re still required in another!)

    The big one: Data/variables that aren’t explicitly shared are CLONED into new threads. This is a performance hit and probably not at all what you intended. The work around is to launch ithreads from a pretty much “pristine” condition (not many modules loaded).

    IIRC, there are modules in the Threads:: namespace that help with making dependencies explicit and/or cleaning up cloned data for new threads.

    Also, IIRC, there’s a slightly different model using ithreads called “Apartment” threads, implemented by Thread::Appartment which has a different usage pattern and another set of trade-offs.

    The upshot:

    Don’t use them unless you know what you’re doing 🙂

    Fork may be more efficient on Unix, but the IPC story is much simpler for ithreads. (This may have been mitigated by CPAN modules since I last looked 🙂

    They’re still better than Python’s threads.

    There might, one day, be something much better in Perl 6.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I made a tic-tac-toe game in c++ want to learn how to use SDL
I want to learn some JVM language, but there are so many EDIT :
I want to learn and use Drupal or Django for the following: dynamic web
I want to learn about how to use sub reports in RDLC in ASP.Net.
I want to learn Guice. I use eclipse. What to download? What to install?
I want to learn and use SBCL because of its ease of learning and
I want to learn to use Fluent NHibernate, and I'm working in VS2010 Beta2,
I want to learn how to use Struts 2 and I created a simple
I want to learn how to use JGoodies binding (since beans binding seems dead
I want to learn to use vim. The thing is simple : I don't

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.