The Royal Bank of Scotland Chennai Interview Question
Software Engineer / DevelopersCountry: United States
Interview Type: In-Person
One thread can wait for input while the other does background work. Without threads, you would not be fully utilizing the system because no background work would be getting done while you're waiting for input, blocked on disk I/O, etc.
Untrue. You can use select(3) (on UNIX and Windows for sockets) or specify the OVERLAPPED flag in CreateFile (on Windows) to do both. For just one example you can check out the mainloop mechanism of the glib library (which runs both on Windows and UNIX (Linux and Mac at the very least)). I don't believe there is any real difference between the two - other than the convenience of not having to specify timeouts by hand. If there was a real difference, I believe threads would have gone into wide use in the 90s.
To be completely honest, I'm not exactly sure what you're talking about, but in any case, I certainly did not mean to imply that there aren't other ways of doing I/O and the like. I'm just saying that this is one way you could decide to use threads.
I think the question is significantly more basic than you take it to be. I think it's asking why you would use threads in a typical program, not what advantages threads have over special non-blocking I/O, etc.
Just take the example of msword . it uses two threads: one for handling rendering and another for managing the UI. The rendering thread effectively runs hidden in the background for spell check etc while the UI thread receives input, handles events, and runs application code.
- aakash01 December 31, 2011considering the advantages of using threads like they are light weight , inter thread communication is easy and a thread context switch is cheap , its better to use multithreaded solution.