#!/usr/bin/perl use strict; use warnings; use Tk; use threads; use threads::shared; my $mw = new MainWindow; my $button = $mw->Button('-relief' => 'raised', '-text' => 'CreateNumbers', '-command' => sub { &createNumberThread(0); }); $button->pack(); MainLoop(); BEGIN { my $nb_thread = undef; my $run :shared = 1; sub createNumberThread { my $nb = $_[0]; # delete currently active thread if one is available if( defined $nb_thread ) { $run = 0; } $nb_thread = threads->new(\&createNumbers, $nb); $nb_thread->detach(); $run = 1; sub createNumbers { my $nb = $_[0]; while($run) { print $nb . "\n"; $nb++; } } } } #### Attempt to free non-existent shared string '_XEvent_', Perl interpreter: 0x1ca69ac at c:/Perl/site/lib/Tk/Widget.pm line 98 during global destruction. Free to wrong pool 1ca6060 not 246f88 at c:/Perl/site/lib/Tk/Widget.pm line 98 during global destruction.