2009年3月17日 星期二

The Downside of MultiThreading

One downside is that thread switching consumes resources. Each time the CPU switches from one thread to another, the state of the current thread must be saved, so it can be resumed later, and the state of the new thread must be retrieved and loaded into the CPU registers. Just adding one extra thread to improve responsiveness is not going to cause any noticeable problems, but if you use, say, 20 threads in a complex statistical analysis program, the extra overhead of task switching may nullify the theoretical advantages of multithreading.

The second downside is that programming multiple threads can be complex. One problem that faces programmers is called a deadlock, when two threads are each waiting for the other to complete, resulting in neither thread ever completing. A related problem, called a race, results when the program's operation depends on which of two or more threads completes first. While .Net provides classes and tools to deal with these and other problems of thread management and synchronization, it remains a complex business.

http://www.informit.com/articles/article.aspx?p=170919&seqNum=2

沒有留言:

張貼留言