#!/usr/bin/perl
use threads;
threads->new(\&my_sub)->detach();
threads->new(\&my_sub)->detach();
sub my_sub {}
####
>perl script.pl
>
####
#!/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);
####
$ script.pl & unix
####
>start /b "" script.pl Windows
####
#!/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;