<br><div class="gmail_quote">On Sun, Jul 11, 2010 at 20:00, div0 <span dir="ltr">&lt;<a href="mailto:div0@users.sourceforge.net">div0@users.sourceforge.net</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">
<div class="im">On 11/07/2010 15:28, Philippe Sigaud wrote:<br>
<br>
<blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">
- Why is a 2 threads version repeatedly thrice as fast as a no thread version?<br>
I thought it&#39;d be only twice as fast.<br>
</blockquote>
<br></div>
Well if you are running on windows, my guess is that your 2nd cpu is completely free of tasks, so the thread running on that one is more efficient. I&#39;m pretty sure that the windows task scheduler trys as much as possible to keep threads running on the last cpu they where on, to reduce cache flushes and memory paging.<br>

<br>
On your first cpu you&#39;ll be paying for the task switching amongst the OS threads &amp; programs. Even if they aren&#39;t using much actual cpu time, there&#39;s still a very high cost to perform the task swaps.<br>
<br>
Your program is obviously very artificial; with a more normal program you&#39;ll see the ratio drop back down to less than twice as fast.<div class="im"><br></div></blockquote><div><br>OK, I get it. Thanks for the explanation!<br>
 </div><blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;"><div class="im">
<br>
<blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">
- It&#39;s fun to see the process running under windows: first only 50% CPU (1<br>
thread), then it jumps to ~100%, while the memory is slowly growing. Then<br>
brutally back to 50% CPU (no thread).<br>
- 1024 threads are OK, but I cannot reach 2048. Why? What is the limit for the<br>
number of spawn I can do? Would that be different if each threads spawn two<br>
sub-threads instead of the main one generating 2048?<br>
</blockquote>
<br></div>
How many did you expect to run? Under &#39;doze each thread by default gets a megabyte of virtual address space for it&#39;s stack. So at about 1000 threads you&#39;ll be using all of your programs 2GB of address then the thread spawn will fail.<br>
</blockquote><div><br>That&#39;s it, I can get much higher than 1000, for 2 GB of RAM.<br>Then it fail with a core.thread.ThreadException: could not create thread.<br><br>I tried this because I was reading an article on Scala&#39;s actors, where they talk about millions of actors. I guess they are quite different.<br>
<br> </div><blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">
<br>
Not sure about linux, but a similar limitation must apply.<br>
<br>
The rule of thumb is don&#39;t bother spawning more threads than you have cpus. You&#39;re just wasting resources mostly.<br></blockquote><div><br>OK. So it means for joe average not much more than 2-8 threads?<br><br>Thanks!<br>
<br>Philippe<br></div></div>