Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When should you use multithreading? And would multi threading be beneficial if the different threads execute mutually independent tasks?

This were the only two questions I couldn't answer in the interview I got rejected from last night.

like image 520
GrowinMan Avatar asked Sep 14 '11 16:09

GrowinMan


People also ask

When should we use multi threading?

Multithreading is a process of executing multiple threads simultaneously. You should use multithreading when you can perform multiple operations together so that it can save time.

Why multithreaded process is better than multiple single threaded processes?

Advantages of Multithreaded ProcessesAll the threads of a process share its resources such as memory, data, files etc. A single application can have different threads within the same address space using resource sharing. It is more economical to use threads as they share the process resources.

What are the advantages of using multithreading instead of multiple processes?

On a multiprocessor system, multiple threads can concurrently run on multiple CPUs. Therefore, multithreaded programs can run much faster than on a uniprocessor system. They can also be faster than a program using multiple processes, because threads require fewer resources and generate less overhead.

Which is better multi threading or multi processing?

Multiprocessing is used to create a more reliable system, whereas multithreading is used to create threads that run parallel to each other. multithreading is quick to create and requires few resources, whereas multiprocessing requires a significant amount of time and specific resources to create.


1 Answers

Q: When should you use multithreading?

A: "Your question is very broad. There are few non-trivial systems where the functionality can be met simply, quickly and reliably with only one thread. For example: [pick out a typical system that the target company sells and pick out a couple aspects of its function that would be better threaded off - heavy CPU, comms, multi-user - just pick out something likely & explain].

Q: Would multithreading be beneficial if the different threads execute mutually independent tasks?

A: "Depends on what you mean by 'executing tasks'. Multithreading would surely be beneficial if the threads process mutually independent data in a concurrent fashion - it reduces requirements for locks and probabilty of deadlocks increases in a super-linear fashion with the number of locks. OTOH, there is no issue with threads executing the same code, this is safe and very common."

like image 188
Martin James Avatar answered Sep 29 '22 10:09

Martin James