ASPN ActiveState Programmer Network
ActiveState
/ Home / Perl / PHP / Python / Tcl / XSLT /
/ Safari / My ASPN /
Cookbooks | Documentation | Mailing Lists | Modules | News Feeds | Products | User Groups


Recent Messages
List Archives
About the List
List Leaders
Subscription Options

View Subscriptions
Help

View by Topic
ActiveState
.NET Framework
Open Source
Perl
PHP
Python
Tcl
Web Services
XML & XSLT

View by Category
Database
General
SOAP
System Administration
Tools
User Interfaces
Web Programming
XML Programming


MyASPN >> Mail Archive >> activeperl
activeperl
RE: threads package missing is_running
by Brian Raven other posts by this author
May 2 2008 9:52AM messages near this date
RE: threads package missing is_running | RE: threads package missing is_running (solvedish)
webmaster@[...].com <mailto:webmaster@[...].com>  wrote:
>  I'd love it if I could just upgrade perl, but corporate bureaucracy
>  sucks and the new versions need approval for security purposes.  I
>  initially tried it with 5.6.1 with less success, and 5.8.0 seems to
>  be very close less the is_running issue.  All I really need is some
>  way to determine if the thread is still running even if it's checking
>  if the process id exists. When it is, I want to SIGUSR1 the thread.  
>  
>  'threads' is a standard module, so I have no clue why it's having
>   amnesia. The module is found at C:/Perl/lib/threads.pm and not part
>  of the ppm. 
>  
>  Here is a short example of threads I'm testing:
>  
>  <xmp>
>  # Test Threads
>  use threads;
>  use threads::shared;
>  use strict;
>  use vars qw ($counter $thr);
>  
>  Log ("Starting Thread Test");
>  $thr = threads->create (\&ChildThread, 0, 1);
>  
>  $counter = 0;
>  while ($counter++ < 10) {
>     Log ("This is the main thread (".$counter." sec)");
>     sleep (1);
>  }
>  
>  # The following gives an error that is_running.al is missing.
>  # Tried threads->list (threads::running) to search and compare: gave
>  bareword error. # Tried "$thr->is_running" without success: also said
>  is_running.al missing. 
>  while ($thr->is_running()) {
>     Log ("Killing thread.");
>     $thr->kill('SIGUSR1');
>     sleep (4);
>  }
>  
>  Log ("Thread killed.");
>  
>  # Functions
>  
>  sub Log { my( $Message ) = @_; print $Message."\n"; }
>  
>  sub ChildThread {
>     my ($counter, $to_screen) = @_;
>     my $count = 0;
>     our $thread_term = 0;
>     $SIG{'SIGUSR1'} = sub { Log('Received user1 notice.');
>     $thread_term = 1; return 0; }; $SIG{'KILL'} = sub { Log('Received
>  kill notice.'); $thread_term = 1; return 0; }; 
>  
>     while (!($thread_term)) {
>        Log ('This is the child thread ('.($count++ * 2).' sec)');
>        sleep (2);
>     }
>     Log ('Thread has ended.');
>  }
>  </xmp>

It occurs to me that you might be using the documentation for a later
version of Perl than you are using. The function is_running is
documented in 5.10.0, but not in an older version (5.8.5) that I have.
Neither is the kill function come to that.

Try something like the following, which seems to work in my older
version.

use strict;
use warnings;
use threads;
use threads::shared;

my $thread_term : shared = 0;

Log ("Starting Thread Test");
my $thr = threads-> create (\&ChildThread, 0, 1);

my $counter = 0;
while ($counter++ < 10) {
   Log ("This is the main thread (".$counter." sec)");
   sleep (1);
}

terminate(1);
$thr-> join();
Log ("Thread killed.");

# Functions

sub Log { my( $Message ) = @_; print $Message."\n"; }

sub ChildThread {
   my ($counter, $to_screen) = @_;
   my $count = 0;

   while (!terminate()) {
      Log ('This is the child thread ('.($count++ * 2).' sec)');
      sleep (2);
   }
   Log ('Thread has ended.');
}

sub terminate {
    lock $thread_term;
    $thread_term = $_[0] if defined $_[0];
    return $thread_term;
}

HTH

-- 
Brian Raven 

=========================================
Atos Euronext Market Solutions Disclaimer
=========================================

The information contained in this e-mail is confidential and solely for the intended address
ee(s). Unauthorised reproduction, disclosure, modification, and/or distribution of this emai
l may be unlawful.
If you have received this email in error, please notify the sender immediately and delete it
 from your system. The views expressed in this message do not necessarily reflect those of A
tos Euronext Market Solutions.

Atos Euronext Market Solutions Limited - Registered in England & Wales with registration no.
 3962327.  Registered office address at 25 Bank Street London E14 5NQ United Kingdom. 
Atos Euronext Market Solutions SAS - Registered in France with registration no. 425 100 294.
  Registered office address at 6/8 Boulevard Haussmann 75009 Paris France.

L'information contenue dans cet e-mail est confidentielle et uniquement destinee a la (aux) 
personnes a laquelle (auxquelle(s)) elle est adressee. Toute copie, publication ou diffusion
 de cet email est interdite. Si cet e-mail vous parvient par erreur, nous vous prions de bie
n vouloir prevenir l'expediteur immediatement et d'effacer le e-mail et annexes jointes de v
otre systeme. Le contenu de ce message electronique ne represente pas necessairement la posi
tion ou le point de vue d'Atos Euronext Market Solutions.
Atos Euronext Market Solutions Limited Société de droit anglais, enregistrée au Royaume U
ni sous le numéro 3962327, dont le siège social se situe 25 Bank Street E14 5NQ Londres Ro
yaume Uni.

Atos Euronext Market Solutions SAS, société par actions simplifiée, enregistré au regist
re dui commerce et des sociétés sous le numéro 425 100 294 RCS Paris et dont le siège so
cial se situe 6/8 Boulevard Haussmann 75009 Paris France.
=========================================

_______________________________________________
ActivePerl mailing list
ActivePerl@[...].com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
Thread:
webmaster
Brian Raven
Brian Raven
Brian Raven
webmaster
Brian Raven
Brian Raven
Bill Luebkert
Andy Bach
webmaster
Andy Bach
Lyle - CosmicPerl.com Support
webmaster
Lyle - CosmicPerl.com Support

Privacy Policy | Email Opt-out | Feedback | Syndication
© 2004 ActiveState, a division of Sophos All rights reserved