1

This question is not a duplicate of any question related to why multithreading is not faster on single-core, read the rest to figure out what I actually want to know

As far as I know, multithreading is only faster on a CPU with multiple cores, since each thread can run in parallel. However, as my understanding of how preemption and multithreading on single-core works, it should also be faster. The image below can describe what I mean better. Consider that our app is a simple loop that takes exactly 4 seconds to execute. In this example, the time slice is constant, but, I don't think it makes any difference because, in the end, all threads with the same priority will get equal time by the scheduler. The first timeline is single-threaded, but the second one has 4 threads. The cycle also means when the preemption ends and the scheduler goes back to the queue of threads from start. I/O has also been removed since that just adds complexity and even if it changes the results, let's assume I'm talking about some code that does not require any sort of I/O.

Multi-threading on single-core real-time timeline

The red threads are threads related to my process, and others (black) are the ones for other processes and apps

There are a couple of questions here:

Why isn't it faster? What's wrong with my timeline?

What's that cycle point called?

Since the time slice is not fixed, does that means the Cycle time is fixed, or the time slice gets calculated and the cycle will be as much time required to spend the calculated time slice on each thread?

Is the slice time based on time or instruction? I mean, is it like 0.1 sec for each thread or like 10 instructions for each thread?

The CPU utilization is based on CPU time, so why isn't it always on 100% because when a thread's time reaches, it moves to the next thread, and if a thread stops on I/O, it does not wait but executes the next one, so the CPU always tries to find a thread to execute and minimalize the time spent IDLE. Is the time for I/O so significant that more than 50% of CPU time is spent doing nothing because all threads are waiting for something, mostly I/O and the CPU time is elapsed waiting for a thread to become in a ready state?

Note: This timeline is simplified, the time spent on I/O, thread creation, etc. is not calculated and it's assumed that other threads do not finish before the end of the timeline and have the same priority/nice value as our process

Mahan Lamee
  • 322
  • 3
  • 12
  • If your first timeline is single-threaded, what are TH2 through TH5 in that timeline? I would expect it to look like this: "TH1 TH1 TH1 TH1" – Jeremy Friesner Sep 27 '21 at 14:30
  • Those are other threads for other background processes – Mahan Lamee Sep 27 '21 at 14:31
  • I think that is your error; usually background processes will be running at lower priority and thus will not slow down the execution speed of your own (presumably non-background) process, i.e. they will run only when your process doesn't need to use the CPU. If there are other processes that are contending for the CPU, the thread(s) in your process will be slowed down, of course, but that's not really predictable and it will happen whether your process is single-threaded or multi-threaded. – Jeremy Friesner Sep 27 '21 at 14:33
  • Let's just assume those are other threads. Like, imagine I have other apps also open too and they have multiple threads with the same priority. Will it then be faster? – Mahan Lamee Sep 27 '21 at 14:34
  • 1
    It might be; it will depend on the scheduling algorithm that the OS is using (note that real-world schedulers are usually much more nuanced than a simple FIFO scheduler would be). If you want to know the behavior for a particular OS+hardware platform, then only real way to find out is to set up a test and measure it. – Jeremy Friesner Sep 27 '21 at 14:35
  • @JeremyFriesner, since I don't have a single-core processor, will a VM give me the results I want? I can create a KVM with 1 core, and test single thread vs multiple, however, I just wanna make sure the hypervisor (KVM) does not somehow use other cores. Also, if you know the answer to any of the other related questions, maybe consider writing a full answer to answer the question. – Mahan Lamee Sep 27 '21 at 14:39
  • I think that would work. – Jeremy Friesner Sep 27 '21 at 14:50
  • IIUC, you expect faster execution because you will get multiple timeslices (one for every thread) on the single processor, effectively stealing the time from other processes? If so, what happens when *every* process starts spawning as many threads as it can, to steal time from all other processes (or to at least mitigate the effects of other processes stealing *their* time)? There's no free lunch, and you probably don't want this kind of abuse to happen on your system, so an OS may start allocating time by process, not by thread (of course, this is when a clever abuser will `fork()` instead). – EOF Sep 27 '21 at 15:24
  • @EOF yep, if you look at it this way, I am stealing time from other processes. Also, I can just make my priority higher and it will do the same thing too. – Mahan Lamee Sep 27 '21 at 16:26

0 Answers0