You were perfectly polite. Sorry that I gave the impression otherwise. My answer was my best reasoning for doing things the way I do.

When you said "semaphores", I assumed you were talking about the threads::shared cond_* apis rather than Thread::Semaphore as the latter is used for serialising access to shared resources rather than signalling. I don't see how you would have a parent thread wait for the termination of it's children using that? If you know, please educate me.

I have used the threads::shared cond_* apis for this purpose in the past but those apis don't seem ideally suited to this purpose either. So far, I have rarely needed their complexity.

I should probably have used lock on $count in my example, but on my single cpu box I have never managed to detect anything that indicated they were necessary for increment and decrements.

Ideally, I should be able to use threads->list to wait for the count of running threads to drop to zero, but it only reports on undetached threads. Which would have worked in this case as I didn't detach the asyncs, but that was an oversight. I normally do.

As for the granularity. I normally use Win32::Sleep for this purpose, as it has millisecond capability, but that's obviously platform specific. I just switched it to sleep cos it's universal.

You could also use select undef, undef, undef, .1 while $count; to reduce the granularity if time is critical. In this case the program was only going to print the results and terminate, so at best, the user might see the first result 0.9 of a second earlier.


Examine what is said, not who speaks.
"Efficiency is intelligent laziness." -David Dunham
"Think for yourself!" - Abigail
"Memory, processor, disk in that order on the hardware side. Algorithm, algorithm, algorithm on the code side." - tachyon

In reply to Re^5: Parallel DNS queries (use threads; ) by BrowserUk
in thread Parallel DNS queries -- please comment on code by Tomte

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.