If I have 8 asynchronous functions to execute and 4 cores in my CPU, What is better (more speedy and correct)?
Start 4 threads and when finish a thread, start another thread, etc.
Start 8 threads
Language aren't important.
The answer is: It depends.
It depends on the workload. If threads are I/O bound, you can benefit by starting all 8 threads. If they're highly CPU bound, then it makes less sense.
Related
For example i5 7600k has 4 threads, but game can have more than 4 threads. What is the difference and why they have the same name?
A CPU that has 4 threads (really a CPU with 4 cores, or possibly a 2 core CPU with Hyperthreading) can execute 4 separate threads simultaneously. A program can have more threads than that, but only 4 of them can be executing at any given time - the others would be in a sleep/wait state while they wait for the CPU to become available.
As for how the CPU "becomes available" for other threads when there are more threads than it can execute at a given time, that's a function of the operating system scheduler. The operating system scheduler rotates threads on and off the CPU periodically (typically every few milliseconds) so that every thread that wants to execute eventually gets its turn on the CPU.
There's more to it than that, but hopefully that covers the gist of your question.
What does this have to do with the threads of the processor? for example, an Intel i5 has four cores and four threads.
How many threads can we use in our program? for example using std :: thread (STL) in C ++?
is 8 threads a big or low number of threads for a program?
It really depends. As a rule of thumb, limit the number of threads to something close to the number of cores (otherwise you might have too much context switchs). You might use std::thread::hardware_concurrency() as a hint. Often, you organize your program with a thread pool.
However, what really matters is the number of active threads. Some programs have hundreds of threads but are organized so that only a few of them are active (i.e. runnable) at any given instant, and most of them being idle (waiting for IO, or for some mutex or condition variable).
Be aware that a thread is a quite heavy resource (in particular because it has its own call stack, usually at least a megabyte). So don't have too many of them (hence, having thousands of threads is generally unreasonable, unless you have a very powerful and expansive computer).
The Intel hyper-threading technology is often disappointing. You probably don't want to have 8 active threads on an Intel processor with 4 cores and 8 hyperthreads. You need to benchmark, but you should expect some bad performance in such case (perhaps having 4 or 6 active threads would make your overall performance better).
Threads are an abstraction provided and managed by the operating system. So read Operating Systems: Three Easy Pieces to understand more about OSes.
If currently kernel is scheduling 60 threads, they belong to 3 processes:
A: 10 threads
B: 20 threads
C: 30 threads
And they are all doing computing (no disk IO)
Will C gets more stuff done than B, and B gets more stuff done than A?
It does not seem very fair to me. If I am a irresponsible programmer, I can just spawn more threads to eat more CPU resource.
How this relate to golang:
In go, typically the go scheduler has a thread pool of #of-CPU-cores threads. Why does this make sense if a process with more threads gets more stuff done?
The situation you describe is an overloaded machine. It is only true that the process with more threads will get more work done if the CPU has no spare time.
Go was not designed to fight other processes for a greater share of an overloaded CPU. You are free to set GOMAXPROCS to any number you like if you desire to participate in such a fight.
In a more typical system where the total work is less than the total CPU time; a Go process with 8 threads and 30 goroutines will perform about equally to a process with 30 threads running at the same time.
It does not seem very fair to me. If I am a irresponsible programmer,
I can just spawn more threads to eat more CPU resource.
you can also allocate your entire free memory, cause OS-failure and hijack the network card. you can do sorts of things but then who will want to consume your softwares?
How this relate to golang: In go, typically the go scheduler has a
thread pool of #of-CPU-cores threads. Why does this make sense if a
process with more threads gets more stuff done?
Golang goroutines are basically a threadpool. each goroutine is a thread-pool work item. many things can make your thread-pool thread block, like using synchronous IO, waiting on a (non-spin-lock) lock and manually sleeping or yielding. in these cases, which are very common, having more threads than CPU's usually increase the performance of your application.
Do note that not all IO is a disk IO. writing to your console is an IO operation, but it isn't really a "Disk IO".
another thing is that context switching may not consume a large portion of your CPU, and having more threads may not make your tasks-throughput degrade. so in this case having more threads means that your parallelism is higher, and yet you don't loose performance. this is somewhat common situation. context switching between threads these days is very cheap. having a bit more threads than your cores may not necessary kill your performance or degrade it in some way.
For multicore computing, one thing confusing me from the beginning is the model of multicore hardware is too abstracted from the real machine.
I worked on a laptop with a single intel processor, containing 4 cores and support hyperthreading which makes the num of logical cores 8.
Suppose I have a Java program implemented a concurrent algorithm (it is said Java would use the OS rule of thread scheduling, so JVM won't affect the scheduling), and the program is purely CPU-bound.
My observations:
if the program(process) running less than 8 threads, parallelism of work increases as the num of threads increasing
when the total threads num is larger than 8, the performance become complicated, but usually no more improvement than running 8 threads; and for some certain algorithm, it is much worse, i.e.time-consumption increased hugely than running 8 threads.
My knowledge of this:
As far as I know, the program I run is treated as a user process by the OS, and if the program created threads to try to gain parallelism, the OS will try to schedule these threads among the cores available.
Any threads of the process on a same core may share the total execution time of the process on that core.
My questions:
Suppose the CPU is only running my program. i.e. no other user processes.
if there is only 1 core of the CPU, the process will get no benefit of parallelism by multi-threading, since the total execution time of the process will not change. Is it true?
if there are more than one cores available, the OS will try to schedule the threads of the process evenly and fairly on different cores, and the process's threads on different cores get their own (extra)
execution time, therefore speeding up. Is it true?
if there are n threads and m cores, where n>m, than some core may run more than 1 thread of the process, which may even harm the speed-up of parallelism because of "context switch" among threads on
the same core and potentially side-effect of threads of the process running at different speed. Is this true?
Thanks very much!
if there is only 1 core of the CPU, the process will get no benefit of parallelism by multi-threading, since the total execution time of the process will not change. Is it true?
Only if you are 100% CPU-bound. If you have I/O waits, multiple threads can help a lot even on a single core.
if there are more than one cores available, the OS will schedule the threads of the process evenly and fairly on different cores
That seems to be up to the discretion of the OS. There could be all kinds of quota and priorities involved.
may even harm the speed-up of parallelism because of "context switch" among threads on the same core
It is true that there is overhead in managing extra threads (not just at the scheduling level, but also in synchronizing and communicating within your application), and if these threads cannot make productive use of otherwise idle CPU cores, then having less threads can actually improve performance.
I've read somewhere that real multi-threading is limited to the number of CPU cores you are running. Does that mean that all you get if you have, say, a dual core CPU, is real multi-threading with just... 2 parallel threads?
Yes, it essentially means that at most 2 threads can run in parallel. Intel processors implement HyperThreading where a single core acts as two cores and can run two threads (mostly) in parallel so in this case you would end up with maximally 2*2=4 concurrent threads, but most OSes distinguish HT cores and handle a DualCore HT as if it had 4 cores. But all this meditation is useless. For performance aspects there is much more to consider than the sheer number of cores. For parallelism as a problem there are multitasking operating systems which simulate large number of parallel threads even on one core. These simulations are perfect in the sense that they can hit cases where any problem/benefit of real parallelism is observable.