thcsoft has asked for the wisdom of the Perl Monks concerning the following question:
the thing with this thread now is, that it has to start performing at least very close to every 1 second, and not to let that value be influenced by the work it has to perform. the answer to that problem might be another thread:while (1) { sleep 1; ... }
i've just been testing this construct with a simple example:while (1) { sleep 1; my $thread = threads->new(\&work_sub); }
one should assume, that this program sleeps for 2 seconds, prints "foo", sleeps for another second, then prints "\n" and goes to sleep again. but what it does is sleeping for 3 seconds at first, then printing "foo\n", and falling asleep for another 2 seconds...#!/usr/bin/perl -w use threads; while (1) { sleep 2; my $thread = threads->new( sub { print "foo"; sleep 1; print "\n" } +); $thread->detach; }
the latter is apparently not the case with my example.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.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: a question on threads
by BrowserUk (Patriarch) on May 11, 2005 at 03:40 UTC | |
by thcsoft (Monk) on May 11, 2005 at 08:47 UTC | |
by BrowserUk (Patriarch) on May 11, 2005 at 09:19 UTC | |
by thcsoft (Monk) on May 12, 2005 at 02:46 UTC | |
by thcsoft (Monk) on May 12, 2005 at 06:34 UTC | |
|
Re: a question on threads
by davidrw (Prior) on May 11, 2005 at 02:30 UTC | |
|
Re: a question on threads
by Errto (Vicar) on May 11, 2005 at 02:41 UTC | |
by thcsoft (Monk) on May 11, 2005 at 03:00 UTC | |
|
Re: a question on threads
by djp (Hermit) on May 11, 2005 at 05:33 UTC | |
by BrowserUk (Patriarch) on May 11, 2005 at 06:16 UTC | |
by djp (Hermit) on May 12, 2005 at 04:10 UTC | |
by thcsoft (Monk) on May 12, 2005 at 05:07 UTC | |
by typobox43 (Initiate) on May 12, 2005 at 15:28 UTC | |
by BrowserUk (Patriarch) on May 12, 2005 at 15:49 UTC | |
|
Re: a question on threads
by salva (Canon) on May 11, 2005 at 12:58 UTC | |
by Roy Johnson (Monsignor) on May 12, 2005 at 02:08 UTC | |
by thcsoft (Monk) on May 12, 2005 at 05:09 UTC |