Multithreading and multicore differences
I have a couple of small questions.
Firstly is there a difference between multithreading and multicore? Are they two completely different things or does multithreading use more than one core if it needs?
Secondly Most cores have two threads but when profiling my app I noticed lots of different threads ranging from thread 128 to thread 3460. What dictates how many threads your computer has?
Thanks
Firstly is there a difference between multithreading and multicore?
Yes.
Multithreading and Multicore are different pieces of terminology that apply to different areas of computing.
Multicore refers to a computer or processor that has more than one logical CPU core, and that can physically execute multiple instructions at the same time. A computer's "core count" is the total number of cores the computer has: computers may have multiple processors, each of which might have multiple cores; the core count is the total number of cores on all of the processors.
Multithreading refers to a program that can take advantage of a multicore computer by running on more than one core at the same time. In general, twice as many cores equals twice as much computing power (for programs that support multithreading) though some problems are limited by factors other than CPU usage; these problems will not experience gains that are as dramatic from multithreading.
It's important to note that performance is not the only reason programs use many threads. More on that later.
Are they two completely different things or does multithreading use more than one core if it needs?
They are related but seperate.
Programs that support multithreading can use more than one core if more than one is available.
Most cores have two threads but when profiling my app I noticed lots of different threads ranging from thread 128 to thread 3460.
The operating system assigns threads numbers so it can keep track of them.
Most programs you will ever run will not need 3400 threads running at once. Also, a running thread will consume all of a core. The only reason your CPU isnt running at 100% all the time is that the operating system knows how to suspend the processor, which basically makes it stop everything and wait until something happens (such as an IO event or a clock tick). Only one thread can run on a core at once. Different threads running is actually just threads jumping onto the CPU and running for short periods of time, then being switched out with other threads which also need to run.
What dictates how many threads your computer has?
The total number of threads in all of your processes. Also, most operating systems impose a hard limit, a maximum number of existing threads that cannot be surpassed.
A process is a program (you probably know this). Multithreading is the process of having more than one thread per process (many processes will not make more than one thread because they do not have to). Windows does not have a hard limit on the number of threads you can create (at least, not since XP. Not going to say anything about w98 and previous), but of course the number of threads you can make is limited by how much memory you have.
You said some programs use multiple threads for reasons other than performance.
Sometimes it's nice to be able to multitask, even if not concurrently.
Sometimes programs need to do specific things at specific times. A commonly cited example is a program with a visible window. That program might be doing intense background number crunching, but it would benefit it if it could still respond to user events, like clicking buttons and resizing it. This can be accomplished with asynchronous processing, which will require your one thread to repeatedly check for GUI work to do at intervals, pause what it's doing, and handle the GUI for a while. Lots of things do it this way.
Another, possibly better way to handle it is with a thread. Your program doesn't have to worry about switching back and forth between number crunching and GUI management, the operating system will manage that for you. Even if you have only one core, you can still run multiple threads, and your OS will do its best to make sure that all of the running threads in all of the running processes get their fair share of CPU time.
The number of cores and number of threads are decoupled. You can have many threads running on a single core, and you can have situations where only one thread runs despite the presence of more cores (although I cannot think of a real life scenario where this would happen). Let's say multicore is a hardware characteristic, whereas the number of threads is something in the domain of the OS and the processes running on it.
Of course, with a single core you cannot have more than one thread running concurrently. The OS has to constantly switch back and forth between threads.
Threading on a single core usually means you can create x number of threads and they will each be given set amount of time to run (thread quantum). When threads are switched this is known as context switching, all this takes some time so one needs to do some benchmarking to find an ideal number of threads to have per core.
If the majority of work is cpu bound there is little point spawning hundreds of threads since this is unlikely to improve performance (in-fact make it worse maybe, remember context switching does not come for free). However doing this for I/O bound work it may help since while the system is busy doing this work, another thread can be given cpu time.
Having physical additional cores means two things can truly run in parallel at the hardware level.
链接地址: http://www.djcxy.com/p/13924.html上一篇: 使用异步和等待关键字的好处
下一篇: 多线程和多核的差异