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

后端 未结 9 990
生来不讨喜
生来不讨喜 2020-12-23 09:06

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

9条回答
  •  生来不讨喜
    2020-12-23 09:47

    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

提交回复
热议问题