This were the only two questions I couldn't answer in the interview I got rejected from last night.
9 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."
-
1
You should use multithreading when you want to perform heavy operations without "blocking" the flow.
Example in UIs where you do a heavy processing in a background thread but the UI is still active.
If the threads execute mutually exclusive tasks it is the best since there is no overhead for synchronization among threads needed
-
1I would say, mutually exclusive tasks which are CPU bound. Because mutually exclusive task which are IO bound would again be like an overhead. Sep 26, 2017 at 19:01
Multithreading is a way to introduce parallelness in your program. In any case if there can be parallel paths (parts which do not depend on result from a other part) in your program, use can make use of it.
Specially with all these multiple core machines now days, this is a feature which one should exploit.
Some examples would be processing of large data where you can divide it in chunks and get it done in multiple threads, file processing, long running I/O works like network data transfers etc.
To your second question, it would be best if the tasks are mutually independent - reasons
- no shared data means no contentions
- no need for any ordered processing (dependency), so each thread can work when have resources
- more easy to implement
You should definitely use multithreading in GUI applications when you invoke time consuming tasks from the main event loop. Same applies for server application that might block while doing the I/O.
For the second question, it is usually yes when you have machine with multiple CPU cores. In this case these independent tasks can be executed in parallel.
You can use multithreading if the tasks can be broken down which can be executed in parallel. Like produce and consume , Validate and save , Read and Validate.
For the second question , Yes, it is beneficial for make a program into Multi threading if they are executing independent tasks.
This article gives very good reasons: https://marcja.wordpress.com/2007/04/06/four-reasons-to-use-multithreading/
To summarize, the reasons are:
- Keep your program responsive.
- Make better use of your CPU. CPU may be blocked by IO or other stuff. While waiting, why not letting other threads use it
- Multiple threads can be scheduled to multiple CPU cores
- Some problems are naturally to be solved by multi-threading. Such solution can simplify your code.
In general, multithreading is used in cases where execution time is throttled/bottlenecked by the CPU as opposed to other areas such as IO. The second question is really quite subjective to the circumstance. For example if they are mutually independent but both do heavy IO, you might not necessarily get a large gain.
-
1Depends. On a single-core CPU, if CPU time is the bottleneck, threading can actually slow you down (as the CPU and OS have to spend time switching between threads). Threading there wins big only when you have jobs that involve waiting on something sporadic and/or slow -- be it GUI events, keyboard input, whatever -- and want other stuff to happen while those jobs are waiting.– cHaoSep 14, 2011 at 16:55
-
1You're right. I made the assumption there are multiple physical cores. Sep 14, 2011 at 17:02
-
I would actually argue that something CPU-intensive is a poor candidate for multi-threading, unless you run exactly as many threads as physical CPU cores. That is a generalization, however.– BrandonNov 7, 2014 at 19:09
Multithreading is used when we can divide our job into several independent parts. For example, suppose you have to execute a complex database query for fetching data and if you can divide that query into sereval independent queries, then it will be better if you assign a thread to each query and run all in parallel. In that way, your final result output will be faster. Again, this is an example when you have the leverage to run mutliple database queries.
And to answer your second question, it is better to have threads for independent tasks. Otherwise, you will have to take care of synchronization, global variables, etc.
When should you use multithreading?
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.
Would multithreading be beneficial if the different threads execute mutually independent tasks?
it is usually yes. Multithreading would usually be beneficial if the different threads execute mutually independent tasks so that it doesn't affect other threads if exception occur in a single thread.