Single thread to multi-threaded application

后端 未结 6 1061
南笙
南笙 2021-01-03 17:05

When we should use threads in our application. In other words, when should I convert a single threaded application to multi-threaded application. Being a developer, I think

6条回答
  •  天命终不由人
    2021-01-03 18:03

    Reasons I can think of off the top of my head ( and I'm sure there are more ) are :
    1. Offloading a batch of work to a worker thread so your program can either continue responding to user input or so you can carry on running other code that doesn't rely on that work.
    2. Handling I/O particularly server and network communication where the response time is variable or unknown.
    3. Parallel processing of data where you can sub-divide the work down into discrete non-dependant units of work
    4. Timer related work i.e 'every 500ms check if x has changed'

    However, switching to multi-threaded or concurrent programming is not without it's pitfalls particularly if those threads need to access shared data hence the number of questions on SO about mutexes and thread synchronisation!

提交回复
热议问题