Should multi-threading be used for tasks which does not involve IO operation

multithreadingoperating systems

I was reading a book on OS and got confused between different terminologies.
I hope i will get my doubt cleared here.
These questions might be very basic for some experts, but i find this platform best suitable for such clarifications.

Processes are run by CPU and this whole operation is managed by OS.
For Single core CPU, at any instant only one process will be running.
CPU saves the state of a process to Process Control Block and starts running another process which was waiting and this switching is so fast that it feels like all processes are running simultaneously.

Need of different threads in a process was felt to get more fine-grained control over process , that means if process is waiting because of some I/O bound operation then even if this process is selected by CPU, it will not do anything.

So, if multiple threads are running under one process, then if one thread takes care of I/O activity , other threads can do some real computation, provided this process is selected by CPU.

Here are my questions:
Is Multi-threading effective only if task involved I/O activity?
Why is Multi-threading preferred over Multi-processing ? (is it because threads will share same memory space? )

Why Multi-core processors are better for multi-threading? (I think they will be better even if you don't use multi-threading, why so excitement in name of multi-threading?). And anyway threads in different processes will not share same memory, so two threads running in different processes on different cores are like running different processes. It is just that they will be parallel, and the real parallel, not the pseudo parallel.

Best Answer

One of the main reasons for using threads even in a completely CPU-bound process is to allow interaction or update progress output via a UI of some sort. While running the calculation in the foreground without allowing interaction would be more efficient, the slight cost of task switching and handling UI events usually outweighs the penalty of forcing a user to wait indefinitely for a process.

Another case where running multiple threads on a single core would be more efficient is when a multi-stage process involves one stage that produces a lot of temporary results that are processed by a later stage. Running these stages in series might exhaust available memory. Running them in parallel allows the second stage to free up memory as it processes results.

Finally, as we add more caches to our architectures, threads can often become idle when a cache miss occurs while accessing memory. This gives some time for another thread to activate and do some work.

The main benefits of multi-threading over multi-processing include

  • shared memory
  • less overhead for context-switching
  • easier abstraction

Most modern operating systems provide some method of sharing memory across processes, too. Even still, allowing the OS or virtual machine to shift threads across multiple processors when available is very appealing. For example, when you move a multi-threaded Java program from a single-core machine to one with multiple cores, the JVM will automatically make use of those other cores.

Related Topic