while (1) {
sleep 1;
...
}
####
while (1) {
sleep 1;
my $thread = threads->new(\&work_sub);
}
####
#!/usr/bin/perl -w
use threads;
while (1) {
sleep 2;
my $thread = threads->new( sub { print "foo"; sleep 1; print "\n" });
$thread->detach;
}
####
join() does three things: it waits for a thread to exit, cleans up
after it, and returns any data the thread may have produced.
But what if you're not interested in the thread's return values,
and you don't really care when the thread finishes? All you want is
for the thread to get cleaned up after when it's done.
In this case, you use the detach() method. Once a thread is
detached, it'll run until it's finished, then Perl will
clean up after it automatically.