in reply to Re^2: multi threading
in thread multi threading

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."

Replies are listed 'Best First'.
Re^4: multi threading
by mude (Novice) on Mar 01, 2008 at 10:51 UTC
    thanks for the overwhelming information guys... actually im tryin to test a dhcp server... sending massive dhcp request... simultaneous if possible.. but since its not... so ill just try to make may script fast and efficient... any suggestions how to accomplish this in perl?
Re^4: multi threading
by mude (Novice) on Mar 01, 2008 at 10:45 UTC

    thanks for the overwhelming information guys...

    actually im tryin to test a dhcp server... sending massive
    dhcp request... simultaneous if possible.. but since its
    not... so ill just try to make may script fast and
    efficient...

    any suggestions how to accomplish this in perl?
      actually im tryin to test a dhcp server

      Ah! So you are the Monk formerly known as drip, who posted Multi Threading help two days ago.

      The answer to your question was clearly indicated in that other thread. Unfortunately, it would appear that that you do not have enough knowledge of the task you are undertaking to recognise it.


      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.

        nop... he is a good friend of mine whos interested to
        this problem too
      I really don't know much details about networking, but there is 23 packages on CPAN when I search for dhcp dhcp, there is probably one for your use there.

      Now, waiting is one of those thing you actually can do a lot of at any instance, so threads or multiple proccesses (fork) makes a lot of sense in this case. To know what your results tell you, you will have to exercise fine controll over the number of concurrent requests to the server, i.e. how often to send another request, Time::HiRes can help with that.

      Try to start programming, and post code and direct questions if/when you're stuck.

      Update: After reading BrowserUk's answer right above; forget what I wrote here and study sundialsvc4's ansver in that other thread. It's very good. cheers