Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re^4: Execute a sub in the background

by egzoti4en (Initiate)
on Feb 01, 2012 at 14:37 UTC ( [id://951231]=note: print w/replies, xml ) Need Help??


in reply to Re^3: Execute a sub in the background
in thread Execute a sub in the background

Yes i'm not sure how to use threads.

I wrote :

my $thr = threads->create(\&status_line());$thr->join();

The sub executes but i'm not able to get the key input.My program is waiting the thread to end. How can i make it not to wait for it.

Replies are listed 'Best First'.
Re^5: Execute a sub in the background
by BrowserUk (Patriarch) on Feb 01, 2012 at 14:58 UTC

    If you start a thread (->create()) and then immediately wait for it to finish (->join), that is the same as not starting a thread at all.

    If you are not interested in the result of the thread, you should detach it.

    The simplest way of doing what I think you are trying to achieve with your snippet above is:

    async( \&status_line )->detach;

    Notice there are no parens () after the name of the sub to be run asynchronously.

    What you have above should have produced several error messages, because you are calling the sub status_line() then attempting to take a reference to its return value, and then pass that as the code reference to the thread, which obviously won't work.


    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.

    The start of some sanity?

Re^5: Execute a sub in the background
by chessgui (Scribe) on Feb 01, 2012 at 14:46 UTC
    Instead of join use detach:
    my $thr = threads->create(\&status_line)->detach;

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (2)
As of 2024-04-19 21:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found