Boost C++ Libraries of the most highly regarded and expertly designed C++ library projects in the world. Herb Sutter and Andrei Alexandrescu, C++ Coding Standards

This is an old version of boost. Click here for the latest version's documentation home page.
C++ Boost

mutex - Mutex


detail/mutex.hpp provides several mutex types that provide a consistent interface for OS-supplied mutex types. These are all thread-level mutexes; interprocess mutexes are not supported.


This header file will try to guess what kind of system it is on. It will auto-configure itself for Win32 or POSIX+pthread systems. To stub out all mutex code, bypassing the auto-configuration, #define BOOST_NO_MT before any inclusion of this header. To prevent ODR violations, this should be defined in every translation unit in your project, including any library files.


namespace details {
namespace pool {

// Only present if on a Win32 system
class Win32_mutex
    Win32_mutex(const Win32_mutex &);
    void operator=(const Win32_mutex &);


    void lock();
    void unlock();

// Only present if on a POSIX+pthread system
class pthread_mutex
    pthread_mutex(const pthread_mutex &);
    void operator=(const pthread_mutex &);


    void lock();
    void unlock();

// Present on all systems
class null_mutex
    null_mutex(const null_mutex &);
    void operator=(const null_mutex &);


    static void lock();
    static void unlock();

// This will be one of the types above
typedef ... default_mutex;

} // namespace pool
} // namespace details


Symbol Table
Symbol Meaning
Mutex Any type defined in this header
t value of type Mutex

Requirements satisfied by mutex
Expression Return Type Assertion/Note/Pre/Post-Condition
m.lock() not used Locks the mutex
m.unlock() not used Unlocks the mutex

Each mutex is always either owned or unowned. If owned, then it is owned by a particular thread. To "lock" a mutex means to wait until the mutex is unowned, and then make it owned by the current thread. To "unlock" a mutex means to release ownership from the current thread (note that the current thread must own the mutex to release that ownership!). As a special case, the null_mutex never waits.


May include the system headers <windows.h>, <unistd.h>, and/or <pthread.h>.

Future Directions

This header will eventually be replaced by a Boost multithreading library.

Valid HTML 4.01 Transitional

Revised 05 December, 2006

Copyright © 2000, 2001 Stephen Cleary (scleary AT jerviswebb DOT com)

Distributed under the Boost Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at