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

I tried using threads but they don't execute in the background or i simply am not using them correct.

for bigger picture you can see the code uploaded on my github account https://github.com/egzoti4en/MPG123FrontEnd.

I'm trying to count the time that the song has lasted and to draw a status line of it. This what i want to do in this sub and that is why i need a sleep activity to make it sleep and then draw using Curses.(the sub is called status_line()

I have alredy done almost everything so GUI is not an option for me now

Replies are listed 'Best First'.
Re^3: Execute a sub in the background
by BrowserUk (Patriarch) on Feb 01, 2012 at 14:02 UTC
    I tried using threads but they don't execute in the background or i simply am not using them correct.

    You do have to do a little more than put use threads; at the top of your code.

    Which is all you've done in the code you posted the link to. (It is preferred that you post your code here, not some other place on the net.)

    Did you read the POD for the threads module? Is there something you read but didn't understand that we could help you with?


    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?

      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.

        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?

        Instead of join use detach:
        my $thr = threads->create(\&status_line)->detach;