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

Hi wizards !

I have a question about threads. Lets say we have a very trivial thread example:

#!/usr/bin/perl use threads; use strict; my $thr = threads->new(\&sub1); sub sub1 { print "hi from thread"; }

Now is there a way for me to control thread $thr from within sub1? Especially since I want to send $thr as an argument to another function from sub1. So I need the full scalar reference to thread object $thr. I basically ask if there is any way to get reference to $thr from within sub1?

e.g. to make it work like this

my $thr = threads->new(\&sub1); sub sub1 { my $self_thread = $(this); #I know this is absolutely wrong ju +st trying to make you understand what I want print "hi from thread $self_thread"; }

Replies are listed 'Best First'.
Re: Accessing Thread within its run
by BrowserUk (Patriarch) on Feb 15, 2012 at 20:03 UTC

    See threads::self()

    my $thrSelf = threads->self();

    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?

      thank you very much - exactly what I needed