Your queue handling is fatally flawed.

Why are you exiting your queue reading loop if there is nothing pending?

do { ... } while ($worker->pending());

If (for example), some other process decides to hammer the disk drive, then your queue population code might stall waiting for access to the disk, at which point your queue will empty and all your worker threads will terminate despite that there are still files to be read from disk.

Also, why are you calling (pullDataFromDbWithDirectory(), bith in your main thread and in all your worker threads. That's very confused.


The normal way to do this is:

  1. Have your queue worker thread queue reading loops terminate when they see undef:
    sub doOperation () { my $ithread = threads->tid(); while( my $folder = $worker->dequeue() ) { print "Read $folder from queue with thread $ithread\n" if $deb +ug; ## Do something useful here... } }
  2. Start your threads before you populate the queue:

    They will all block on the dequeue() until something is made available.

  3. Populate your queue from you main thread (*ONLY*);

    The threads will start doing work as soon as your main thread gives them something to work on.

  4. Once the main thread has finished populating the queue, it then queues one undef per worker thread to cause the worker loops to terminate and thus the worker threads to end.

    You are queing undefs, but you're not doing it until you've already seen that your threads have ended, at which point it serves no purpose.

  5. Finally, loop over the thread handles calling join() to ensure all the threads have finished before you exit the program.

This way, the whole process becomes self-managing and you don;t have to poll to count threads.


With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

In reply to Re: Proper undefine queue with multithreads by BrowserUk
in thread Proper undefine queue with multithreads by sanc

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.