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.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: multi threading
by mude (Novice) on Mar 01, 2008 at 10:51 UTC | |
|
Re^4: multi threading
by mude (Novice) on Mar 01, 2008 at 10:45 UTC | |
by BrowserUk (Patriarch) on Mar 01, 2008 at 11:17 UTC | |
by mude (Novice) on Mar 03, 2008 at 09:24 UTC | |
by BrowserUk (Patriarch) on Mar 03, 2008 at 09:36 UTC | |
by drip (Beadle) on Mar 04, 2008 at 10:14 UTC | |
by mude (Novice) on Mar 04, 2008 at 10:18 UTC | |
by stiller (Friar) on Mar 01, 2008 at 11:35 UTC |