Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: Re: Multi-Threading in Perl

by Anonymous Monk
on Dec 25, 2001 at 22:07 UTC ( [id://134297]=note: print w/replies, xml ) Need Help??


in reply to Re: Multi-Threading in Perl
in thread Multi-Threading in Perl

Threads are much more efficient. Fork() duplicate the entire process, thus needing A LOT more resources. It is also harder to have 2+ forks() communicate (PIPE or socket or shared memory) vs direct access to all data structures (this of course can lead to other problems though.)

Replies are listed 'Best First'.
Re^3: Multi-Threading in Perl
by Aristotle (Chancellor) on Dec 25, 2001 at 23:01 UTC
    Depends. The real cost is usually not so much that of process creation as that of context switching while running. On most OSs, it is a lot cheaper to switch threads within a process, than switch between processes.

    Linux' context switching however is so fast (at least on Intel CPUs) that it can easily masquerade as threading and indeed in that kernel, "threads" are really just forked processes, with a bit of decoration to hide the separation.

    So the answer really depends on what you're going to run on. If you're primarily going to run on a modern Unix, fork is usually easier to work with than threads and it has much fewer pitfalls, like paying attention to who is accessing what variables and the likes which you run into when working with threads.
Re: Re: Re: Multi-Threading in Perl
by Aighearach (Initiate) on Dec 26, 2001 at 03:39 UTC
    On Linux, and most *nix, threads do not copy the entire process. Only a very little is copied, and the rest is shared until the page it is on changes. That is why Linux can fork() faster than windows can create a new thread. So, forking or threading is a platform dependent issue. Which is why you shouldn't use the non-portable method (threads) in a language like Perl that is usually very portable. fork() should work on all platforms.
    --
    Snazzy tagline here
Re: Re: Re: Multi-Threading in Perl
by LunaticLeo (Scribe) on Dec 26, 2001 at 22:04 UTC
    Threads are much more efficient...It is also harder to have 2+ forks() communicate (PIPE or socket or shared memory) vs direct access to all data structures

    For the love of all that is holy why do you youngsters think this stuff. Free your mind (from Win32s land) and your code will follow.

    Efficiantcy: fork() + SysV shared memory is just as fast on Linux as threads. Further, you get the advantage of explicitly shared data, rather than the joys of tracking down obscure memory corruption bugs.

    Easy of Use: Unix process calls + SysV shared memory API are far more simple than the POSIX thread API and more useful to learn overall.

    Threads are faster for context switching than processes on old propietary unixen/winnt. That is because all those proprietary OSes were created to sell hardware. Why should they care about speeding up the common case?

    Unix is an amazing API. With proper implementation it is awsomely powerful, fast, and simple. Threads are a context switching optimization/abomination.

    However, perl threads could make POE really cool on multi-processor machines (re: thread per CPU to implement an explicit state machine).

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://134297]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (7)
As of 2024-04-25 15:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found