Giter Site home page Giter Site logo

mutexpp's Introduction

Build Status

mutexpp is a collection of C++ mutex implementations and utilities.

Overview

What is a blocking mutex?

A mutex that, upon locking, will halt a thread's execution until that mutex is acquired.

These types of mutexes are best used when the critical sections they protect are relatively long (such as I/O or OS API calls).

Pros

When blocked, the thread consumes almost no computer resources.

Cons

It is very slow to transition a blocking mutex from unlocked to locked.

Examples

std::mutex

What is a spin mutex?

A mutex that, upon locking, will loop a thread's execution until that mutex is acquired.

These types of mutexes are best used when the critical sections they protect are relatively brief (such as memory-resident data modifications).

Pros

When blocked, the thread will spike a CPU.

Cons

It is very fast to transition a spin mutex from unlocked to locked.

Examples

tbb::spin_mutex

What is a 'hybrid' mutex?

A mutex that, upon locking, will first spin, then halt a thread's execution until that mutex is acquired. The amount of time taken during the initial spin is adjusted such that the lock acquistion happens more often during the spin phase.

These mutexes are best used when spin mutexes would suffice, but occasionally a critical section may take an unusually long amount of time, and it would be better to block the thread.

Pros

Tries to maximize throughput while minimizing computer resource consumption. The spin phase is an attempt to avoid the expensive cost of blocking the thread, while the blocking phase attempts to avoid the expense of spiking a CPU in the event locking is taking too long.

Cons

If the time required to lock the mutex is consistently large (i.e., beyond the time it takes to block/unblock a thread) you'll be spending time spinning when you're better off using a blocking mutex.

Notes

Process scheduling is not considered.

Adaptive computation derivation

After every mutex lock, we adjust the expected spin time. Given $p_n$ to be the predictive spin time and $m$ to be the measured spin time,

$$ \begin{align} p_{n+1} &= \frac{7}{8} p_n + \frac{1}{8} m \\ 8 p_{n+1} &= 7 p_n + m \\ 8 p_{n+1} &= 8 p_n + m - p_n \\ p_{n+1} &= p_n + \frac{m - p_n}{8} \\ \end{align} $$

See Also

Mutex related

  • A description of pthread's adaptive mutex from the original implementer, Kaz Kylheku
  • Intel TBB description of mutex flavors.

Clock & Timing related

mutexpp's People

Contributors

fosterbrereton avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.