Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

How to use multithreading in perl?

by Anonymous Monk
on Apr 12, 2014 at 17:59 UTC ( [id://1082113]=perlquestion: print w/replies, xml ) Need Help??

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

Hi, I am C++ developer and now learning and implementing multithreading in perl. But i found most of the examples jsut tell the basics only. Any real working example solving a pblm will help me a lot. Thanks

Replies are listed 'Best First'.
Re: How to use multithreading in perl?
by zentara (Archbishop) on Apr 12, 2014 at 19:54 UTC
    There are just some very general comments one can make about Perl threads.

    The first is not all modules are thread safe, so be cautious when using module code across threads. Many modules are fine though, it depends. Google if in doubt.

    Second, since Perl scripts are run by interpreter, memory allocations made by a thread, will be kept by the mother process for later use. So memory gains can be problem. So if you need to reclaim memory during the whole process, it is better to fork.

    Third, since Perl threads are made by a copy-on-create method, where the thread is a copy of the mother process at the time of creation, the threads can get alot of useless crud in them, and tend to be slow starting up.

    Fourth, it pays to design to reuse your threads. Make the threads once, then just reset them and feed them new data. This is a general way of improving performance and avoiding problems which come from spawning many many threads. But sometimes spawing and detaching works fine. YMMV

    Fifth, Perl threads are best suited for situations where you need realtime sharing of data between threads, using shared thread variables. Any other use can usually be done cleaner with a fork.

    Signals can be tricky. The mother thread receives all signals, and it is up to you to pass it on to the correct thread.

    I'm just an amateur, and I tend to manually control my threads, but are some useful examples of mine and other monks: Using Select and pipes to communicate with threads .... using the thread->kill() feature on linux .... Interrupting a blocking read (Linux::Inotify2) with a signal handler within a thread ... How to wake a thread in Perl .... Simple threaded chat server ... Re: Passing objects between threads...any solutions? (shared objects) .... Reusable threads demo

    Those ought to get you started. :-) Good luck.

    P.S. As you google for examples, look for the answers from BrowserUk. He has the best understanding.


    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh
Re: How to use multithreading in perl?
by BrowserUk (Patriarch) on Apr 13, 2014 at 00:50 UTC
    Hi, I am C++ developer and now learning and implementing multithreading in perl. But i found most of the examples jsut tell the basics only. Any real working example solving a pblm will help me a lot. Thanks

    Hi. I am Perl developer and now learning and implementing multi-threading in C++. Any real working example solving a pblm will help me a lot.

    My point: What kind of problem are you seeking to tackle?

    Because if you are expecting that any single example of threading in Perl is going to show you everything; you're sh*t outta luck.

    Multi-threading an IO-bound problem requires a completely different approach to multi-threading a CPU-bound problem.

    An 'embarrassingly parallel' task might be trivial to implement in Perl, if the dataset easily partitions vertically; however, it might be nearly impossible to code efficiently if the only way to partition the dataset is horizontally.

    If you can define your actual problem in clear and concise terms; providing sufficient information, clear and unambiguous estimates of scale; and real(istic) data samples; and an idea of where you are having problems in coding a solution; then we may be able to begin making suggestions for your direction.


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: How to use multithreading in perl?
by toolic (Bishop) on Apr 12, 2014 at 18:02 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1082113]
Front-paged by LanX
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (5)
As of 2024-04-23 07:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found