A single CPU can only execute one piece of code at a time. No matter how many threads or processes you use, only one will be doing anything at any given moment. It is only the ability of the OS to switch between different threads/processes at relatively high speeds that give the impression of many things are happening at once.

If your machine has 2 processors, then 2 threads can be doing something at exactly the same time. But, if your process has two threads, it is very unlikely that each of those threads would be running on a separate processor at exactly the same moment. This is because your process threads have to compete with the threads of every other process running on that machine. And most OSs have anything from a few tens to several hundred threads running in 'system processes'. Ie. those programs and drivers that make up the OS itself that you have no control over.

So, even if your machine has one processor per thread you intend to run, expecting to get even two threads to run "simultaneously" is impractical, if not impossible.

The best you could hope to achieve is to arrange for your threads to become eligible to run at the same moment using the cond_wait()/cond_signal()/cond_broadcast() APIs documented in threads::shared, and then allow the scheduler to run them as close together as it can. But you will probably need to acquire considerably more understanding of Perl and threads before you will be able to achieve optimum results. And even then, they will still not be running "simultaneously".

The best you will be able to achieve is number of threads * task-switch-time / number of CPUs. You'll have identical or worse results using processes.

Or, you could try describing what you are actually trying to achieve with your program, and perhaps someone can suggest a practical alternative.


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.
"Too many [] have been sedated by an oppressive environment of political correctness and risk aversion."

In reply to Re^3: multi threading by BrowserUk
in thread multi threading by mude

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.