Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

thread ids stringifying wrong in v5.12.4?

by samwyse (Scribe)
on Dec 14, 2012 at 14:52 UTC ( [id://1008849]=perlquestion: print w/replies, xml ) Need Help??

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

I want to fire off a dozen threads, and keep track of which one is doing what in case one of them hangs. (They are all running diagnostic commands on networking equipment.) Unfortunately, I can't seem to save the thread ids in any useful way. Here's sample code:
#! perl -slw use 5.012_000; use strict; use threads; use Data::Dumper; my %toc; for my $i (1..3) { my $id = async { sleep 3*$i+5; $i }; print Dumper($id); $toc{$id} = "starting thread $id, with \$i = $i"; } print Dumper(\%toc); sub joinable { threads->list( threads::joinable ) }; sub running { threads->list( threads::running ) }; while (my $count = scalar running) { sleep 1 until joinable; print "$toc{$_} => ", $_->join, "\n" for joinable; } print "all post-commands are complete\n";
When I run the above, I get back this, which looks like my thread ids are getting converted to "threads=XXX" when I try to use them as hash keys:
$VAR1 = bless( do{\(my $o = '25730180')}, 'threads' ); $VAR1 = bless( do{\(my $o = '26709332')}, 'threads' ); $VAR1 = bless( do{\(my $o = '27250780')}, 'threads' ); $VAR1 = { 'threads=SCALAR(0x187b454)' => 'starting thread threads=SCAL +AR(0x187b4 54), with $i = 3', 'threads=SCALAR(0x193d404)' => 'starting thread threads=SCAL +AR(0x193d4 04), with $i = 1', 'threads=SCALAR(0x193d5b4)' => 'starting thread threads=SCAL +AR(0x193d5 b4), with $i = 2' }; Use of uninitialized value within %toc in concatenation (.) or string +at test.pl line 25. => 1 Use of uninitialized value within %toc in concatenation (.) or string +at test.pl line 25. => 2 Use of uninitialized value within %toc in concatenation (.) or string +at test.pl line 25. => 3 all post-commands are complete
Anyone got any ideas? Thanks!

Replies are listed 'Best First'.
Re: thread ids stringifying wrong in v5.12.4?
by BrowserUk (Patriarch) on Dec 14, 2012 at 15:07 UTC

    async does not return a "thread id", it returns a blessed thread handle. To get a thread id from a thread handle, use $threadHandle->tid;

    Thus, there is no perpose in storing your thread handles in a hash -- where they will get stringified -- because you can (and do) obtain those handles from your joinable() sub.

    Your while loop for joining the threads is horrible -- it obtains the same list over and over. It can be replaced by a simple:

    printf "%d => %d\n", $_->tid, $_->join for @threads;

    Provided you store your thread handles in the array @threads so:

    push @threads, async { sleep 3*$i+5; $i };

    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.

    RIP Neil Armstrong

      Strictly speaking, I wasn't storing the thread handles in a hash, I was wanting to use them as keys. This is because I wanted to associate information for each thread (i.e. the parameters used when started). Using the tid() method solved that problem.

      I'm unsure that your code for joining the threads is useful to me, as it seems that it would wait for the first thread to finish before checking any others. Since some of the threads may hang, this would not solve my stated problem. I do agree that using a sleep loop isn't optimal, and I should look for a better way to wait for threads to exit.

        some of the threads may hang

        How do you handle not getting the return value from those threads that hang?

        How will you clean those threads up when your program ends?


        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.

        RIP Neil Armstrong

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (3)
As of 2024-04-20 03:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found