0

Suppose I create 10 new Task in a loop and run on 2 core processor, maximum how many threads can run in parallel. Is it 2 because all I have is 2 core processor? Is it correct that there can be more than 2 threads created but only 2 threads run in parallel and there will be context switching with other threads?

  • 1
    No, Yes, & Yes. – Enigmativity Feb 16 '20 at 08:20
  • 1
    Essentially yes, but it's complicated. A thread that is sleeping or awaiting a lock or I/O doesn't take up a core, it just isn't scheduled. Hyptherthreading allows more threads of execution than cores. "False sharing" can cause cores to serialize their execution if their onboard caches overlap main memory addresses. There are probably other nuances I don't know of. – John Wu Feb 16 '20 at 08:28
  • Are you aware about the conceptual differences between *concurrently* and *in parallel*? – Theodor Zoulias Feb 16 '20 at 08:59
  • 1
    *"there will be context switching with **other** threads"*, well, there will be context switching with *all* threads. And there will always be, even if you have just 1 thread, because the operating system needs to do things too. – hyde Feb 16 '20 at 10:05
  • @TheodorZoulias I think they are same? – Vivek Acharya Feb 16 '20 at 19:14
  • 1
    [They are different](https://stackoverflow.com/questions/1897993/what-is-the-difference-between-concurrent-programming-and-parallel-programming). Parallelism is about moments in time, while concurrency is about time durations. You can't have parallelism in a single-core machine, because in each moment only one thread can be running. But you can have concurrency, because two time durations can overlap. – Theodor Zoulias Feb 16 '20 at 21:30

1 Answers1

3

The number of processors used to execute a program is defined as the degree of parallelism. So if you have n processors then only n threads could be run in parallel, all the other threads will be context switching.

Lior
  • 508
  • 3
  • 18