Quote:Original post by Promit
Consider that the primary reason for a thread to stall is a hit to memory or L2 cache. These latencies can be rather a lot of cycles (on the order of tens or even hundreds for main memory). Remember also that an OS can schedule a new thread in that gap in constant time. Also, realize that typical code run on a Pentium 4 spends massive amounts of time (20%-50%) waiting for L2 or main memory. Combine all these factors and what you get is that the threads can very efficiently fill in the stalls. This has dimishing returns, obviously, so for any given code, CPU, main memory, etc. you'll find various optimal points. But even for a single CPU, multiple threads can do better, context switches and all.
But as the number of threads grows, the likelihood of cache misses grows as each thread is hitting memory.
Quote:Original post by Omaha
Looking for performance bottlenecks in things that you can't control (z.b. how long it takes for threads to swap out) is not the right way to attack optimization, and neither is attacking it before you even know there's a problem. Just make intelligent design choices and if performance becomes an issue later in development, deal with it then, otherwise the only bottleneck you're going to have is a development stall when you drive yourself batty trying to find ways to cut corners that are already round.
I disagree completely. It's very important to find performance bottlenecks you can't control at the beginning. If you have no control over something, you better plan for it from the beginning because you will be unable to change it later. It's those kinds of changes that can cause a massive re-write and a monumental waste of time.
In the real world of this example, the thred situation will most likely not be an issue; however, if the OP is planning on using 1000 threads, he should do some research first to determine whether or not it will become an issue before going further down the road.