in reply to Suppressing thread warnings
For starters, one way to silence the warning (since threads 1.33?) is to detach the threads.
#!/usr/bin/perl use threads; threads->new(\&my_sub)->detach(); threads->new(\&my_sub)->detach(); sub my_sub {}
>perl script.pl >
All it does is silence the warning. The underlying cause is still present, and it's quite serious. Your threads are being forcibly exited, doing no cleanup whatsoever. For example, the following code produces two empty files.
#!/usr/bin/perl use threads; sub my_sub { open(my $fh, '>', "$0.".threads->tid() ) or die; print $fh 'text'; sleep(10); } threads->new(\&my_sub)->detach(); threads->new(\&my_sub)->detach(); sleep(2);
If you wish to run your program in the background, either do it from the shell
$ script.pl & unix
>start /b "" script.pl Windows
or using something like:
#!/usr/bin/perl use strict; use warnings; BEGIN { # Move to running in background. if (!@ARGV || $ARGV[0] ne '--nobkg') { $SIG{CHLD} = 'IGNORE'; require IPC::Open3; IPC::Open3::open3( '<&STDIN', '>&STDOUT', '>&STDERR', $^X, $0, '--nobkg', @ARGV ); exit; } } use threads; sub my_sub { open(my $fh, '>', "$0.".threads->tid() ) or die; print $fh 'text'; sleep(10); } my $thread1 = new threads(\&my_sub); my $thread2 = new threads(\&my_sub); $_->join() for $thread1, $thread2;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Suppressing thread warnings
by BrowserUk (Patriarch) on Oct 11, 2008 at 01:31 UTC | |
by ikegami (Patriarch) on Oct 11, 2008 at 04:34 UTC | |
by BrowserUk (Patriarch) on Oct 11, 2008 at 07:07 UTC | |
by ikegami (Patriarch) on Oct 11, 2008 at 11:36 UTC | |
by BrowserUk (Patriarch) on Oct 11, 2008 at 11:59 UTC | |
|