Be aware, threads do not run concurrently unless you have multiple cpus.

Even using a non-polling event (such as cond_broadcast), each of the threads will be started sequentially, but there is no guarentee of what order they will be started.

When each thread receives it's signal to start, it will the run for a complete timeslice (barring yields). The timeslice will vary from OS to OS, but typically might be somewhere between 3 and 20 milliseconds. With 10 threads, that means the last thread will have no chance of starting until at least .03 of a second after the first. Modern cpus can process a huge amount of data in this time. A crude test with a 2GHz cpu shows perl can iterate a loop incrementing a scalar 1_000_000 times or perform at least 500_000 exponentiations in this time.

Finally, there is no guarentee--in fact it is extremely unlikely--that your pool of threads will run on adjacent timeslices. Remember that the threads in your process are competing with every other thread in the system for timeslices, including all the high and real-time priority threads which will take presedence. In fact, it is almost impossible that your threads will get even numbers of timeslices. The usual pattern is that some threads will tend to dominate, and the other threads will be starved of CPU until one or more of the dominate threads terminates or goes into IO wait states.

The bottom line is that there is simply no way to synchronise threads. Indeed, any design that tried to synchronise threads is fundementally flawed.


Examine what is said, not who speaks.
"Efficiency is intelligent laziness." -David Dunham
"Think for yourself!" - Abigail

In reply to Re: Thread Synchronization Mechanism by BrowserUk
in thread Thread Synchronization Mechanism by pbeckingham

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.