Multithreading Programming Considerations

Multithreaded Programming Notes

         1, a clear purpose, why use multithreading? If it is the read and write single-threaded or network access bottlenecks (eg HTTP access to the Internet), you can consider using thread pool. If it is for different resources (eg SOCKET connection) management, you may consider multiple threads.

          2 threads in mind, how to control scheduling and blocking threads, such as the use of a trigger event to control scheduling and blocking threads, is also useful to control the message.

          3, if the use of public resources in the thread, the thread must consider the safety of public resources. LOCK lock mechanism is generally used to control the thread safety. Make sure not to have a deadlock mechanism.

          4, the rational use of sleep, when to Sleep, Sleep according to the specific size of the project, to make reasonable arrangements. Under general principles of non-blocking state of each cycle should have SLeep, thus ensuring reduction snatch threads on the CPU. Every thread of readiness and activation will take up some resources available, if the thread body if there are multiple cycles, multiple use SLEEP will result in slower performance.

          5, terminate the thread to make the thread of the general body in the case of the completion of a job termination, generally do not directly use the thread exception is thrown terminate the thread.

          6, need to have a whole plan in accordance with the priority of certain program threads.

Guess you like

Origin blog.csdn.net/weixin_40654382/article/details/86634658