Windows System Programming Third Edition [Electronic resources]

Johnson M. Hart

نسخه متنی -صفحه : 291/ 144
نمايش فراداده

  • Performance Guidelines and Pitfalls

    Multiple threads can provide significant programming advantages, including simpler programming models and performance improvement. However, there are several performance pitfalls that can have drastic and unexpected negative performance impact, and the impact is not always consistent on different computers, even when they are running the same Windows version. Some simple guidelines, summarizing the experience in this chapter, will help to avoid these pitfalls. Some of these guidelines are adapted from Butenhof's Programming with POSIX Pthreads, as are many of the designing, debugging, and testing hints in the next chapter.Program 9-1.

  • Locking is expensive; use it only as required. Hold (own) a mutex or CS only as long as required and no longer. Setting the TimedMutualExclusion delay or sleep point parameters shows the impact of holding a lock for a long period of time.

  • Use distinct mutexes for distinct resources so that locking is as granular as possible. In particular, avoid global locks.

  • High lock contention hinders good performance. The greater the frequency of thread locking and unlocking, and the larger the number of threads, the greater the performance impact. Performance degradation can be drastic and is not just linear in the number of threads.

  • CSs provide an efficient, lightweight locking mechanism when the number of contending threads is small, but mutexes sometimes provide better performance. When using CSs in a performance-critical SMP application, tune performance with the CS spin counts.

  • Semaphores can reduce the number of active contending threads without forcing you to change your programming model.

  • SMP can cause severe, often unexpected, performance impacts in cases where you might expect improved performance. Reducing contention and using thread affinity are techniques to maintain good performance.

  • Choices such as whether to use a signal or broadcast model, explained in Chapter 10, also affect performance significantly.

  • Investigate using commercially available profiling and performance analysis tools, which can help clarify the behavior of the threads in your program and locate time-consuming code segments.