Do threads by default run on more than one core?
In multi-core processors, and windows application runs many threads. do the threads by default run on more than one core ? I mean every thread might run on individual开发者_JAVA技巧 core.
Edit:
If my application runs many threads, all these threads will run on one process.
Without parallel programming..
Is the process able to devide and run on many core ?
Does my application get benifit of multi-core processing ?
(Your question is fairly unclear. I hope I've answered what you were asking, but if you can clarify the question that would help.)
It's up to the OS where a thread is scheduled. There are advantages to keeping a thread on the same core if possible, in terms of cache coherency etc — but forcing it to stay on the same core is usually overly restrictive.
In short: yes, a thread can run on different cores. Not at the same time, of course - it's only one thread of execution — but it could execute on core C0 at time T0, and then on core C1 at time T1.
EDIT: You talk about an application running many threads, but "without parallel programming" — that's a contradiction in terms. If you're using many threads, you are using parallel programming, and by default those threads can run on multiple cores. If you're only using one thread, then you won't get any benefit from having multiple cores — other than the fact that other processes can use the other cores, of course.
When you start multiple threads, they may, or may not, run on different cores.
You can, if you absolutely must, force a thread to run a specific core by using the SetProcessorAffinity method of the Thread class. You really shouldn't do this unless you know why you need to do it.
It is up to the operating system to schedule threads.
To best take advantage of multiple cores, there are plenty of tutorials and knowledge articles out on the web, but here are some tips:
- Divide up your work into smaller pieces, this avoids having really long-running threads that hog a single CPU
- Try to avoid data contention, try to make sure each thread/task gets all the data it needs up front, and returns all the data it produces out the other end, instead of it having to go and talk to a data structure. This avoids locking problems or race conditions.
- Use the runtime to help you, don't try to invent the wheel again. If you're in .NET 4.0, you should look at the TPL, Task Parallel Library, which will help you tremendously.
- Lastly, multi-threading is hard, don't jump into this thinking you can wing it and learn as you go. Read up on the subject, study example code, learn about the pitfalls and how to avoid them.
No, for that reason Microsoft released .NET Parallel API:
- http://www.codeproject.com/KB/Parallel_Programming/NET4ParallelIntro.aspx
This API has been designed just for making multi-core, parallel programming easier than before.
people seem to forget that a single core contains multiple ALU's and that although humans generate linear programming code, in reality multiple instructions can be done at the same time and that's not even considering the pipeline. Basically, cpu manufacturers need to figure out a way to make 1 pseudo core out of multiple cores not just the other way around.
精彩评论