Multithreaded Programming Guide

Exit Print View

Updated: July 2014
 
 

Locking Guidelines

Follow these simple guidelines for locking.

  • Try not to hold locks across long operations like I/O where performance can be adversely affected.

  • Do not hold locks when calling a function that is outside the module and might reenter the module.

  • In general, start with a coarse-grained approach, identify bottlenecks, and add finer-grained locking where necessary to alleviate the bottlenecks. Most locks are held for short amounts of time and contention is rare. So, fix only those locks that have measured contention.

  • When using multiple locks, avoid deadlocks by making sure that all threads acquire the locks in the same order.