# Boost.Threads - Header <boost/thread/mutex.hpp>

###

# Boost.Threads

## Header <boost/thread/mutex.hpp>

## Contents

Introduction
Classes

Class `mutex`

Class `mutex` synopsis
Class `mutex` constructors and destructor

Class `try_mutex`

Class `try_mutex` synopsis
Class `try_mutex` constructors and destructor

Class `timed_mutex`

Class `timed_mutex` synopsis
Class `timed_mutex` constructors and destructor

Example(s)

## Introduction

Include the header <boost/thread/mutex.hpp> to define the `mutex`, `try_mutex` and `timed_mutex` classes.

The `mutex`, `try_mutex` and `timed_mutex` classes are models of Mutex, TryMutex, and TimedMutex respectively. These types should be used to non-recursively synchronize access to shared resources. For recursive locking mechanics, see the recursive mutexes supplied by Boost.Threads.

Each class supplies one or more typedefs for lock types which model matching lock concepts. For the best possible performance you should use the mutex class that supports the minimum set of lock types that you need.

Mutex Class | Lock name | Lock Concept
`mutex` | `scoped_lock` | ScopedLock
`try_mutex` | `scoped_lock scoped_try_lock` | ScopedLock ScopedTryLock
`timed_mutex` | `scoped_lock scoped_try_lock scoped_timed_lock` | ScopedLock ScopedTryLock ScopedTimedLock

The `mutex`, `try_mutex` and `timed_mutex` classes use an `Unspecified` locking strategy, so attempts to recursively lock them or attempts to unlock them by threads that don't own a lock on them result in undefined behavior. This strategy allows implementations to be as efficient as possible on any given platform. It is, however, recommended that implementations include debugging support to detect misuse when `NDEBUG` is not defined.

Like all the Boost.Threads mutex models, the `mutex`, `try_mutex` and `timed_mutex` leave the scheduling policy as `Unspecified`. Programmers should make no assumptions about the order in which waiting threads acquire a lock.

## Classes

### Class `mutex`

The `mutex` class is a model of Mutex and NonCopyable, and provides no additional facilities beyond the requirements of these concepts.

#### Class `mutex` synopsis

    namespace boost
    {
        class mutex : private boost::noncopyable // Exposition only.
            // Class mutex meets the NonCopyable requirement.
        {
        public:
            typedef [implementation defined; see Introduction] scoped_lock;

            mutex();
            ~mutex();
        };
    };

#### Class `mutex` constructors and destructor

    mutex();

Postconditions: `*this` is in an unlocked state.

    ~mutex();

Requires: `*this` is in an unlocked sate.
Danger: Destruction of a locked mutex is a serious programming error resulting in undefined behavior such as a program crash.

### Class `try_mutex`

The `try_mutex` class is a model of TryMutex and NonCopyable, and provides no additional facilities beyond the requirements of these concepts.

#### Class `try_mutex` synopsis

    namespace boost
    {
        class try_mutex : private boost::noncopyable // Exposition only.
            // Class try_mutex meets the NonCopyable requirement.
        {
        Public:
            typedef [implementation defined; see Introduction] scoped_lock;
            typedef [implementation defined; see Introduction] scoped_try_lock;

            try_mutex();
            ~try_mutex();
        };
    };

#### Class `try_mutex` constructors and destructor

    try_mutex();

Postconditions: `*this` is in an unlocked state.

    ~try_mutex();

Requires: `*this` is in an unlocked sate.
Danger: Destruction of a locked mutex is a serious programming error resulting in undefined behavior such as a program crash.

### Class `timed_mutex`

The `timed_mutex` class is a model of TimedMutex and NonCopyable, and provides no additional facilities beyond the requirements of these concepts.

#### Class `timed_mutex` synopsis

    namespace boost
    {
        class timed_mutex : private boost::noncopyable // Exposition only.
            // Class timed_mutex meets the NonCopyable requirement.
        {
        Public:
            typedef [implementation defined; see Introduction] scoped_lock;
            typedef [implementation defined; see Introduction] scoped_try_lock;
            typedef [implementation defined; see Introduction] scoped_timed_lock;

            timed_mutex();
            ~timed_mutex();
        };
    };

#### Class `timed_mutex` constructors and destructor

    timed_mutex();

Postconditions: `*this` is in an unlocked state.

    ~timed_mutex();

Requires: `*this` is in an unlocked sate.
Danger: Destruction of a locked mutex is a serious programming error resulting in undefined behavior such as a program crash.

## Example(s)

    #include <boost/thread/mutex.hpp>
    #include <boost/thread/thread.hpp>
    #include <iostream>

    boost::mutex io_mutex; // The iostreams are not guaranteed to be thread-safe!

    class counter
    {
    public:
        counter() : count(0) { }

        int increment() {
            boost::mutex::scoped_lock scoped_lock(mutex);
            return ++count;
        }

    private:
        boost::mutex mutex;
        int count;
    };

    counter c;

    void change_count()
    {
        int i = c.increment();
        boost::mutex::scoped_lock scoped_lock(io_mutex);
        std::cout << "count == " << i << std::endl;
    }

    int main(int, char*[])
    {
        const int num_threads = 4;
        boost::thread_group thrds;
        for (int i=0; i < num_threads; ++i)
            thrds.create_thread(&change_count);

        thrds.join_all();

        return 0;
    }

The output is:

    count == 1
    count == 2
    count == 3
    count == 4

Revised 05 November, 2001

© Copyright William E. Kempf 2001-2002. All Rights Reserved.

Permission to use, copy, modify, distribute and sell this software and its documentation for any purpose is hereby granted without fee, provided that the above copyright notice appear in all copies and that both that copyright notice and this permission notice appear in supporting documentation. William E. Kempf makes no representations about the suitability of this software for any purpose. It is provided "as is" without express or implied warranty.