Boost C++ Libraries

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

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

mutex - Mutex

Introduction

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.

Configuration

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.

Synopsis

namespace details {
namespace pool {

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

  public:
    Win32_mutex();
    ~Win32_mutex();

    void lock();
    void unlock();
};

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

  public:
    pthread_mutex();
    ~pthread_mutex();

    void lock();
    void unlock();
};

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

  public:
    null_mutex();
    ~null_mutex();

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

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

} // namespace pool
} // namespace details

Semantics

Symbol Table
SymbolMeaning
MutexAny type defined in this header
tvalue of type Mutex

Requirements satisfied by mutex
ExpressionReturn TypeAssertion/Note/Pre/Post-Condition
m.lock()not usedLocks the mutex
m.unlock()not usedUnlocks 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.

Dependencies

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.


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

This file can be redistributed and/or modified under the terms found in copyright.html

This software and its documentation is provided "as is" without express or implied warranty, and with no claim as to its suitability for any purpose.