Home


Dennis Lang
lang.dennis @ comcast.net

Measure Various Locking Strategies
Updated: 31-Aug-2009

The following table shows the results of timing various locking strategies with and without contention from competing threads. I also measure the cost to malloc memory with and without contention. The locking strategies are:

Windows Linux
Interlock Atomic instructions
Critical Section Pthread Mutex

The test measures 8 operations in one or two threads:

  • Counting in one thread, sleep(0) in 2nd thread.
  • Shared counting in two threads
  • Interlock (atomic instructions to set/test a variable) in one thread, sleep(0) in 2nd thread.
  • Interlock with two threads trying to set/test the same variable.
  • Pthread mutex (Linux) or Critical section (Windows) in single thread, sleep(0) in 2nd thread.
  • Mutex or Critical section with two theads sharing lock.
  • Memory allocation (malloc) in single thread, sleep(0) in 2nd thread.
  • Malloc occuring in two threads.
  • The following graph plots the relative performance of each run. The results are relative to test#1, which times how long it takes to increment a value in one thread while the 2nd thread was doing nothing.

    Observations:

  • Shared counting is more expensive than using an interlock (atomic) instruction.
  • Interlocks or atomic instructions are fast both with and without contention compared to Mutex/Critical sections.
  • Mutex or Critical sections are expensive (as you would expect) and should be avoided.
  • Linux and Centos have faster 'malloc' than Windows, relative to time measured in test#1.
  • Data table and graphs created using Excel:

    The table is sorted in performance order (fastest to slowest) per Hardware configuration.
    The table includes both Windows and Linux results.
    * Click on column heading to sort. Supports multi-column sorts in order sorts applied.

    SecondsTest Description Hardware
    0.292112 Time interlock Dell 670 2 dual/core 3 Ghz Linux 2.5? kernel
    0.333655 Time couting in two threads Dell 670 2 dual/core 3 Ghz Linux 2.5? kernel
    0.365504 Time couting Dell 670 2 dual/core 3 Ghz Linux 2.5? kernel
    0.381036 Time interlock with collison Dell 670 2 dual/core 3 Ghz Linux 2.5? kernel
    0.503537 Time mutex/critical_section Dell 670 2 dual/core 3 Ghz Linux 2.5? kernel
    0.541135 Time malloc Dell 670 2 dual/core 3 Ghz Linux 2.5? kernel
    0.565152 Time malloc with contention Dell 670 2 dual/core 3 Ghz Linux 2.5? kernel
    0.588693 Time mutex/critical_section with collison Dell 670 2 dual/core 3 Ghz Linux 2.5? kernel
     
    0.154479 Time couting Dell 690 Centos
    0.167945 Time interlock Dell 690 Centos
    0.215153 Time interlock with collison Dell 690 Centos
    0.286555 Time malloc Dell 690 Centos
    0.308464 Time mutex/critical_section Dell 690 Centos
    0.327101 Time couting in two threads Dell 690 Centos
    0.452401 Time malloc with contention Dell 690 Centos
    0.844744 Time mutex/critical_section with collison Dell 690 Centos
     
    0.094684 Time couting HP 4 dual/core 2.8 Ghz Intel/Xeon Windows XP32 Professional (cl /D WIN32 /MD /Zi /O2)
    0.107344 Time interlock HP 4 dual/core 2.8 Ghz Intel/Xeon Windows XP32 Professional (cl /D WIN32 /MD /Zi /O2)
    0.124729 Time mutex/critical_section HP 4 dual/core 2.8 Ghz Intel/Xeon Windows XP32 Professional (cl /D WIN32 /MD /Zi /O2)
    0.195459 Time interlock with collison HP 4 dual/core 2.8 Ghz Intel/Xeon Windows XP32 Professional (cl /D WIN32 /MD /Zi /O2)
    0.212798 Time malloc HP 4 dual/core 2.8 Ghz Intel/Xeon Windows XP32 Professional (cl /D WIN32 /MD /Zi /O2)
    0.259603 Time couting in two threads HP 4 dual/core 2.8 Ghz Intel/Xeon Windows XP32 Professional (cl /D WIN32 /MD /Zi /O2)
    0.763054 Time mutex/critical_section with collison HP 4 dual/core 2.8 Ghz Intel/Xeon Windows XP32 Professional (cl /D WIN32 /MD /Zi /O2)
    1.223511 Time malloc with contention HP 4 dual/core 2.8 Ghz Intel/Xeon Windows XP32 Professional (cl /D WIN32 /MD /Zi /O2)
     
    0.109211 Time couting HP 4 dual/core 2.8 Ghz Intel/Xeon Windows XP32 Professional (cl /D WIN32 /MD /Zi)
    0.123599 Time interlock HP 4 dual/core 2.8 Ghz Intel/Xeon Windows XP32 Professional (cl /D WIN32 /MD /Zi)
    0.141559 Time mutex/critical_section HP 4 dual/core 2.8 Ghz Intel/Xeon Windows XP32 Professional (cl /D WIN32 /MD /Zi)
    0.213573 Time interlock with collison HP 4 dual/core 2.8 Ghz Intel/Xeon Windows XP32 Professional (cl /D WIN32 /MD /Zi)
    0.214477 Time malloc HP 4 dual/core 2.8 Ghz Intel/Xeon Windows XP32 Professional (cl /D WIN32 /MD /Zi)
    0.276162 Time couting in two threads HP 4 dual/core 2.8 Ghz Intel/Xeon Windows XP32 Professional (cl /D WIN32 /MD /Zi)
    0.961129 Time mutex/critical_section with collison HP 4 dual/core 2.8 Ghz Intel/Xeon Windows XP32 Professional (cl /D WIN32 /MD /Zi)
    1.188000 Time malloc with contention HP 4 dual/core 2.8 Ghz Intel/Xeon Windows XP32 Professional (cl /D WIN32 /MD /Zi)
     
    0.093182 Time couting HP 4 dual/core 2.8 Ghz Intel/Xeon Windows XP32 Professional (cl /D WIN32 /Mt /O2)
    0.106709 Time interlock HP 4 dual/core 2.8 Ghz Intel/Xeon Windows XP32 Professional (cl /D WIN32 /Mt /O2)
    0.156504 Time interlock with collison HP 4 dual/core 2.8 Ghz Intel/Xeon Windows XP32 Professional (cl /D WIN32 /Mt /O2)
    0.159648 Time mutex/critical_section HP 4 dual/core 2.8 Ghz Intel/Xeon Windows XP32 Professional (cl /D WIN32 /Mt /O2)
    0.203873 Time malloc HP 4 dual/core 2.8 Ghz Intel/Xeon Windows XP32 Professional (cl /D WIN32 /Mt /O2)
    0.257813 Time couting in two threads HP 4 dual/core 2.8 Ghz Intel/Xeon Windows XP32 Professional (cl /D WIN32 /Mt /O2)
    0.793430 Time mutex/critical_section with collison HP 4 dual/core 2.8 Ghz Intel/Xeon Windows XP32 Professional (cl /D WIN32 /Mt /O2)
    1.268256 Time malloc with contention HP 4 dual/core 2.8 Ghz Intel/Xeon Windows XP32 Professional (cl /D WIN32 /Mt /O2)
     
    0.230114 Time couting Windows XP - Laptop M1710
    0.254910 Time interlock Windows XP - Laptop M1710
    0.286393 Time mutex/critical_section Windows XP - Laptop M1710
    0.338414 Time interlock with collison Windows XP - Laptop M1710
    0.443224 Time mutex/critical_section with collison Windows XP - Laptop M1710
    1.691142 Time malloc Windows XP - Laptop M1710


  • Download source code: Dennis Lang's TimeLocks.zip (Linux and Windows)

  • Top
    Home