<?xml version="1.0" encoding="windows-1252"?>
<node id="1008042" title="Re^4: removing all threads.." created="2012-12-10 00:42:10" updated="2012-12-10 00:42:10">
<type id="11">
note</type>
<author id="171588">
BrowserUk</author>
<data>
<field name="doctext">
&lt;blockquote&gt;&lt;i&gt;but I've found it difficult to do in the past without Threads::Shared which has some serious limitations. &lt;/i&gt;&lt;/blockquote&gt;

&lt;p&gt;Hm. Seems simple enough to me?:&lt;code&gt;
#! perl -slw
use strict;
use threads;
use threads::shared;

my $sem :shared;
sub tprint {
    lock $sem;
    print @_;
}

my $dieNow :shared = 0;
$SIG{INT} = sub {
    tprint 'sigint seen';
    $dieNow = 1;
    return;
};

sub thread {
    my $tid = threads-&gt;tid;
    my $delay = int rand 10;
    my $next = time + $delay;
    while( sleep 1 ) {
        last if $dieNow;
        redo unless time &gt; $next;
        tprint "[$tid] ping";
        $next = time + $delay;
    }
    tprint "$tid] ending";
}

my @threads = map async( \&amp;thread ), 1 .. 10;

sleep 1 until $dieNow;
$dieNow = 1;

$_-&gt;join for @threads;
&lt;/code&gt;

&lt;p&gt;Outputs:&lt;code&gt;
C:\test&gt;1007974
[3] ping
[9] ping
[1] ping
[3] ping
[2] ping
[4] ping
[3] ping
[1] ping
[9] ping
[5] ping
[3] ping
[6] ping
[10] ping
sigint seen
[3] ping
7] ending
2] ending
5] ending
8] ending
1] ending
9] ending
4] ending
6] ending
10] ending
3] ending
&lt;/code&gt;

&lt;div class="pmsig"&gt;&lt;div class="pmsig-171588"&gt;
&lt;hr /&gt;
&lt;font size=1 &gt;
&lt;div&gt;With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'&lt;/div&gt;
&lt;div&gt;Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.&lt;/div&gt;
&lt;div&gt;"Science is about questioning the status quo. Questioning authority". &lt;/div&gt;
&lt;div&gt;In the absence of evidence, opinion is indistinguishable from prejudice.
&lt;p align=right&gt; [http://thebottomline.cpaaustralia.com.au/|RIP Neil Armstrong]&lt;/p&gt;&lt;/div&gt;
&lt;/font&gt;

&lt;/div&gt;&lt;/div&gt;</field>
<field name="root_node">
1007974</field>
<field name="parent_node">
1008008</field>
</data>
</node>
