hi all,
i'm working on a server process, which, for various reasons, has to be multithreaded. on of these threads has quite a lot to do, but only every second:
while (1) { sleep 1; ... }
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; my $thread = threads->new(\&work_sub); }
i've just been testing this construct with a simple example:
#!/usr/bin/perl -w use threads; while (1) { sleep 2; my $thread = threads->new( sub { print "foo"; sleep 1; print "\n" } +); $thread->detach; }
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...
what startles me even more is that $thread->detach seems to have no influence at all. if commented out, the program behaves like before.

in perlthrtut it says:
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.
the latter is apparently not the case with my example.
since this is a vital problem for my current project, any productive comments will be highly appreciated.
many thanx in advance. :)

language is a virus from outer space.

In reply to a question on threads by thcsoft

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.