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

I want to detach $thr_1 from $thr_2 when $timer is up. But it seems not work by following code.

Does anyone know how to achieve this function? Thanks very much!

#! /usr/bin/perl use warnings; use strict; use threads; my $thr_1; my $thr_2; my $timer = 3; my $res; $thr_1 = threads->new('func_thr_1'); $thr_2 = threads->create('func_thr_2'); $res = $thr_1->join(); print "exit code is: $res\n"; print "\nend\n"; sub func_thr_1 { my $cc = 0; print "thread 1 is running\n"; while ($cc <= 10) { print "cout is $cc\n"; sleep 1; $cc++; } return 'success'; } sub func_thr_2 { while ($timer >= 0) { $timer--; sleep 1; } $thr_1->detach(); print "\nTime up\n"; }
  • Comment on how can I detach a thread from another or how can I set a timer for a thread?
  • Download Code

Replies are listed 'Best First'.
Re: how can I detach a thread from another or how can I set a timer for a thread?
by zentara (Cardinal) on Nov 10, 2011 at 12:31 UTC
    I'm not sure what you are trying to do, first you join $thr_1, then later you want to detach it. ???? You can't do both. Here is an example of a timer.
    #!/usr/bin/perl -w use strict; use threads; use threads::shared; my $timer_go:shared = 0; my $worker = threads->create(\&worker); # detaching the timer my $timer = threads->create(\&timer,$worker)->detach(); print "hit enter to start\n"; <>; $timer_go=1; while( (scalar threads->list) > 0 ){ print scalar threads->list,"\n"; sleep 1; foreach my $thread (threads->list) { if( $thread->is_joinable ){ $thread->join;} } } print "worker joined, all done\n"; exit; sub timer { my $worker = shift; while(1){ if($timer_go){ my $count = 0; while(1){ $count++; if($count > 5){ print "timed out\n"; # Send a signal to a thread $worker->kill('INT'); return; # will destroy a detached thread } sleep 1; print "timing $count\n"; } }else{sleep 1} } } sub worker { $|++; $SIG{INT} = sub{ warn "Caught Zap!\n"; threads->exit() }; # threads->exit() just exits the thread only while(1){sleep 1; next} return; }

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh

      I wish I had updated threads module, but my co. just has old version perl, v5.8.8 built for x86_64-linux-thread-multi. So I cannot use such exit() kill() or sort of.

      Thank you all the same.

Re: how can I detach a thread from another or how can I set a timer for a thread?
by Khen1950fx (Canon) on Nov 10, 2011 at 12:59 UTC
    At line #35
    $thr_1->detach();
    Delete that line. Then on line #12 add detach:
    $thr_2 = threads->create('func_thr_2')->detach;
    The script:
    #!/usr/bin/perl use strict; use warnings; use threads; my $thr_1; my $thr_2; my $timer = 3; my $res; $thr_1 = threads->new('func_thr_1'); $thr_2 = threads->create('func_thr_2')->detach; $res = $thr_1->join(); print "exit code is: $res\n"; print "\nend\n"; sub func_thr_1 { my $cc = 0; print "thread 1 is running\n"; while ($cc <= 10) { print "cout is $cc\n"; sleep 1; $cc++; } return 'success'; } sub func_thr_2 { while ($timer >= 0) { $timer--; sleep 1; } print "\nTime up\n"; }

      Sorry, I misunderstand detach(). Actually, what I want is terminating $thr_1 when time's up in $thr_2

      I have to write it with an old perl version v5.8.8 built for x86_64-linux-thread-multi. So there is no exit() or kill() available in the module.

        Sorry, I misunderstand detach()

        When a thread reaches the end of it's code block, or is issued a return, it sits and waits with it's return values, for the main thread to join it back in. When you detach the thread, the main thread no longer keeps count of it, and dosn't wait around for any return values. When a detached thread ends, it just goes away, instead of sitting there waiting to be joined.

        If you are using the old 5.8.8 version of threads, you will have to do a bit more work, because, as you say, many thread methods are missing in 5.8.8. You will have to rely on older hacks and work-arounds, for instance see Re: Backticks and SIGALRM


        I'm not really a human, but I play one on earth.
        Old Perl Programmer Haiku ................... flash japh