# C++11 Synchronization

The Intel® Threading Building Blocks (Intel® TBB) library approximates a portion of C++11 interfaces for condition variables and scoped locking. The approximation is based on the C++11 working draft N3000. The major differences are:

- The implementation uses the `tbb::tick_count` interface instead of the C++11 <chrono> interface.

- The implementation throws `std::runtime_error` instead of a C++11 std::system_error.

- The implementation omits or approximates features requiring C++11 language support such as `constexpr` or `explicit` operators.

- The implementation works in conjunction with tbb::mutex wherever the C++11 specification calls for a `std::mutex`.

See the working draft N3000 for a detailed descriptions of the members.

### Caution

Implementations may change if the C++11 specification changes.

### Caution

When support for `std::system_error` becomes available, implementations may throw `std::system_error` instead of `std::runtime_error`.

The library defines the C++11 interfaces in namespace std, not namespace tbb, as explained in Section std Namespace.

## Header

    #include "tbb/compat/condition_variable"

## Members

    namespace std {
            struct defer_lock_t { };
            struct try_to_lock_t { };
            struct adopt_lock_t { };
            const defer_lock_t defer_lock = {};
            const try_to_lock_t try_to_lock = {};
            const adopt_lock_t adopt_lock = {};

            template<typename M>
            class lock_guard {
            public:
                typedef M mutex_type;
                explicit lock_guard(mutex_type& m);
                lock_guard(mutex_type& m, adopt_lock_t);
                ~lock_guard();
            };

            template<typename M>
            class unique_lock: no_copy {
            public:
                typedef M mutex_type;

                unique_lock();
                explicit unique_lock(mutex_type& m);
                unique_lock(mutex_type& m, defer_lock_t);
                unique_lock(mutex_type& m, try_to_lock_t));
                unique_lock(mutex_type& m, adopt_lock_t);
                unique_lock(mutex_type& m, const tick_count::interval_t &i);
                ~unique_lock();

                void lock();

                bool try_lock();
                bool try_lock_for( const tick_count::interval_t &i );

                void unlock();

                void swap(unique_lock& u);

                mutex_type* release();

                bool owns_lock() const;
                operator bool() const;
                mutex_type* mutex() const;
            };

            template<typename M>
            void swap(unique_lock<M>& x, unique_lock<M>& y);

            enum cv_status {no_timeout, timeout};

            class condition_variable : no_copy {
            public:
                condition_variable();
                ~condition_variable();

                void notify_one();
                void notify_all();

                void wait(unique_lock<mutex>& lock);

                template <class Predicate>
                void wait(unique_lock<mutex>& lock, Predicate pred);

                cv_status wait_for(unique_lock<mutex>& lock,
                                   const tick_count::interval_t& i);

                template<typename Predicate>
                bool wait_for(unique_lock<mutex>& lock,
                              const tick_count::interval_t &i,
                              Predicate pred);

                typedef implementation-defined native_handle_type;
                native_handle_type native_handle();
            };
        } // namespace std

Parent topic: Synchronization

## See Also

Namespace std