Reputation: 31
If there are n threads (T1, T2...Tn
) running n
different tasks in parallel in a single processor and time to complete each task by each thread is (t1, t2...tn
) respectively then won't the total time taken to complete all the tasks be Ttotal = (t1+t2+..tn) + (total context switch time)
in stead of Ttotal = max(t1, t2..tn) + (context switch time)
?
I know that introducing threads reduces the total waiting time but does it reduce the total processing time of all tasks by any chance ? I doubt it because i know that there is no concept in which a single processor can run multiple tasks at the same time unit.
Upvotes: 1
Views: 604
Reputation: 286
The total time taken will be Ttotal = (t1+t2+..tn) + (total context switch time)
, because each task T(i) will need t(i) units of time.
However, since all of these tasks are disjoint and different from each other, they occur in parallel amongst the n
threads and can be executed independently from each other. Therefore, in reality, when using multithreading, it actually appears that the total time taken is Ttotal = max(t1, t2..tn) + (context switch time)
.
Upvotes: 0
Reputation: 594
that's partial right. you just take CPU/RAM consuming threads into account, in that case, single thread is the best solution. but if there are threads waiting for slow resources, like HDD (even SSD is relatively slow to RAM) or network or a keyboard, things are different. waiting threads do not need CPU time, so we steal their time and give to other threads for free. that's why we need multithreading.
Upvotes: 2