Contention among threads

Contention occurs when one thread has to wait for another thread to finish using a resource.

Contention problems can occur if your application uses too few mutual exclusions (mutexes) to protect access to a large number of resources. A large number of threads that share a small number of resources can also cause contention between threads in your application.

Contention between threads over resources can cause context switches and paging. To reduce contention within your application, you should hold locks for the shortest amount of time and try to prevent a single lock from being used for two different or unrelated shared resources.

Threads that poll or spin to wait for resources prevent scaling. This form of contention can drastically affect the performance of your application. Polling or spinning can also adversely affect other threads or jobs, degrading system performance. Use condition variables, semaphores, mutexes, or other synchronization primitives so that threads never need to poll or spin to wait for resources to become available.