take a good class on operating systems if you really want to understand all the details.

in the meantime, here's my best shot at an explanation:

threading allows for concurrency. concurrency means doing more than one thing at a time. the benefits of doing more than one thing at a time should be fairly obvious.

of course, perl already has a form of concurrency with fork(), which spawns a new child process, so why would we care about threading? spawning processes is inefficient and cumbersome in many situations. spawning a process on most operating systems requires copying large chunks of memory and modifying process tables; this is slow. then, once the new processes are spawned, they're totally seperate; they can't share data very easily without the use of inter-process communications (IPC) which are tricky things to handle. making seperate processes work together on a single task gets mind-bogglingly difficult very quickly. (don't think i'm bashing fork() here; spawning new processes is a perfectly good way of accomplishing many tasks. i use fork() all the time.)

so threading comes in for situations where you want to have lots of things going on at the same time, probably with a good deal of communication and cooperation between all of the seperate tasks (threads). it's probably simplest to think of threads as lightweight processes; the process of spawning a new thread is very similar to spawning a new process but involves far less copying and fewer calls to the operating system itself (which tend to have expensive context-switches). threads are also generally allowed to all access the same data (with some provisions for synchronization) which makes communication and cooperation significantly easier.

if that's not terribly clean, let me know and i'll elaborate.

anders pearson // digital samurai
personal      // http://www.columbia.edu/~anders/
weblog       // http://thraxil.dhs.org/

In reply to Re: threading perl by thraxil
in thread threading perl by tekniko

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.