Note: this document is a proposed Working Draft of the Concurrency TS.

Refer to N4107 for the last approved Working Draft.

Document Number:N4399
Date:
Editor: Artur Laksberg
Microsoft Corp.
arturl@microsoft.com

Working Draft, Technical Specification for C++ Extensions for Concurrency

Note: this is an early draft. It’s known to be incomplet and incorrekt, and it has lots of bad formatting.

1

General

[general]
1.1

Namespaces, headers, and modifications to standard classes

[general.namespaces]

Since the extensions described in this technical specification are experimental and not part of the C++ standard library, they should not be declared directly within namespace std. Unless otherwise specified, all components described in this technical specification either:

  • modify an existing interface in the C++ Standard Library in-place,
  • are declared in a namespace whose name appends ::experimental::concurrency_v1 to a namespace defined in the C++ Standard Library, such as std, or
  • are declared in a subnamespace of a namespace described in the previous bullet, whose name is not the same as an existing subnamespace of namespace std.

Each header described in this technical specification shall import the contents of std::experimental::concurrency_v1 into std::experimental as if by

namespace std {
  namespace experimental {
    inline namespace concurrency_v1 {}
  }
}

Unless otherwise specified, references to other entities described in this technical specification are assumed to be qualified with std::experimental::concurrency_v1::, and references to entities described in the standard are assumed to be qualified with std::.

Extensions that are expected to eventually be added to an existing header <meow> are provided inside the <experimental/meow> header, which shall include the standard contents of <meow> as if by

#include <meow>

New headers are also provided in the <experimental/> directory, but without such an #include.

Table 1 — C++ library headers
  • <experimental/future>
1.2

Future plans (Informative)

[general.plans]

This section describes tentative plans for future versions of this technical specification and plans for moving content into future versions of the C++ Standard.

The C++ committee intends to release a new version of this technical specification approximately every year, containing the library extensions we hope to add to a near-future version of the C++ Standard. Future versions will define their contents in std::experimental::concurrency_v2, std::experimental::concurrency_v3, etc., with the most recent implemented version inlined into std::experimental.

When an extension defined in this or a future version of this technical specification represents enough existing practice, it will be moved into the next version of the C++ Standard by removing the experimental::concurrency_vN segment of its namespace and by removing the experimental/ prefix from its header's path.

1.3

Feature-testing recommendations (Informative)

[general.feature.test]

For the sake of improved portability between partial implementations of various C++ standards, WG21 (the ISO technical committee for the C++ programming language) recommends that implementers and programmers follow the guidelines in this section concerning feature-test macros. [ Note: WG21's SD-6 makes similar recommendations for the C++ Standard itself. end note ]

Implementers who provide a new standard feature should define a macro with the recommended name, in the same circumstances under which the feature is available (for example, taking into account relevant command-line options), to indicate the presence of support for that feature. Implementers should define that macro with the value specified in the most recent version of this technical specification that they have implemented. The recommended macro name is "__cpp_lib_experimental_" followed by the string in the "Macro Name Suffix" column.

Programmers who wish to determine whether a feature is available in an implementation should base that determination on the presence of the header (determined with __has_include(<header/name>)) and the state of the macro with the recommended name. (The absence of a tested feature may result in a program with decreased functionality, or the relevant functionality may be provided in a different way. A program that strictly depends on support for a feature can just try to use the feature unconditionally; presumably, on an implementation lacking necessary support, translation will fail.)

Table 2 — Significant features in this technical specification
Doc. No. Title Primary Section Macro Name Suffix Value Header
N3875 Improvements to std::future<T> and Related APIs 2 future_continuations 201410 <experimental/future>
2

Improvements to std::future<T> and Related APIs

[futures]
2.1

General

[futures.general]

The extensions proposed here are an evolution of the functionality of std::future and std::shared_future. The extensions enable wait free composition of asynchronous operations. Class templates std::promise and std::packaged_task are also updated to be compatible with the updated std::future.

2.2

Header <experimental/future> synopsis

[header.future.synop]
#include <future>

namespace std {
  namespace experimental {
  inline namespace concurrency_v1 {

    template <class R> class promise;
    template <class R> class promise<R&>;
    template <> class promise<void>;

    template <class R>
      void swap(promise<R>& x, promise<R>& y) noexcept;

    template <class R> class future;
    template <class R> class future<R&>;
    template <> class future<void>;
    template <class R> class shared_future;
    template <class R> class shared_future<R&>;
    template <> class shared_future<void>;

    template <class> class packaged_task; // undefined
    template <class R, class... ArgTypes>
      class packaged_task<R(ArgTypes...)>;

    template <class R, class... ArgTypes>
      void swap(packaged_task<R(ArgTypes...)>&, packaged_task<R(ArgTypes...)>&) noexcept;

    template <class T>
      future<decay_t<T>> make_ready_future(T&& value);
    future<void> make_ready_future();

    future<T> make_exceptional_future(exception_ptr ex);
    template <class T, class E>
      future<T> make_exceptional_future(E ex);

    template <class InputIterator>
      see below when_all(InputIterator first, InputIterator last);
    template <class... Futures>
      see below when_all(Futures&&... futures);

    template <class Sequence>
    struct when_any_result;

    template <class InputIterator>
      see below when_any(InputIterator first, InputIterator last);
    template <class... Futures>
      see below when_any(Futures&&... futures);

  } // namespace concurrency_v1
  } // namespace experimental

  template <class R, class Alloc>
    struct uses_allocator<experimental::promise<R>, Alloc>;

  template <class R, class Alloc>
    struct uses_allocator<experimental::packaged_task<R>, Alloc>;

} // namespace std
2.3

Class template future

[futures.unique_future]

The specification of all declarations within this sub-clause 2.3 and its sub-clauses are the same as the corresponding declarations, as specified in C++14 §30.6.6, unless explicitly specified otherwise.


namespace std {
  namespace experimental {
  inline namespace concurrency_v1 {

    template <class R>
    class future {
    public:
      future() noexcept;
      future(future &&) noexcept;
      future(const future&) = delete;
      future(future<future<R>>&&) noexcept;
      ~future();
      future& operator=(const future&) = delete;
      future& operator=(future&&) noexcept;
      shared_future<R&> share();

      // retrieving the value
      see below get();

      // functions to check state
      bool valid() const noexcept;
      bool is_ready() const noexcept;

      void wait() const;
      template <class Rep, class Period>
        future_status wait_for(const chrono::duration<Rep, Period>& rel_time) const;
      template <class Clock, class Duration>
        future_status wait_until(const chrono::time_point<Clock, Duration>& abs_time) const;

      // continuations
      template <class F>
        see below then(F&& func);

    };

  } // namespace concurrency_v1
  } // namespace experimental
  } // namespace std

In C++14 §30.6.6 between paragraphs 9 and 10, add the following:

future(future<future<R>>&& rhs) noexcept;
Effects:
Constructs a future object from the shared state referred to by rhs. The future becomes ready when one of the following occurs:
  • Both the rhs and rhs.get() are ready. The value or the exception from rhs.get() is stored in the future's shared state.
  • rhs is ready but rhs.get() is invalid. An exception of type std::future_error, with an error condition of std::future_errc::broken_promise is stored in the future's shared state.
Postconditions:
  • valid() == true.
  • rhs.valid() == false.

After C++14 §30.6.6 paragraph 26, add the following:

The member function template then provides a mechanism for attaching a continuation to a future object, which will be executed as specified below.


template <class F>
see below then(F&& func);
Requires:
INVOKE(DECAY_COPY (std::forward<F>(func)), std::move(*this)) shall be a valid expression.
Effects:
The function creates a shared state that is associated with the returned future object. The further behavior of the function is defined below.
  • When the object's shared state is ready, the continuation INVOKE(DECAY_COPY(std::forward<F>(func)), std::move(*this)) is called on an unspecified thread of execution with the call to DECAY_COPY() being evaluated in the thread that called then.
  • Any value returned from the continuation is stored as the result in the shared state of the resulting future. Any exception propagated from the execution of the continuation is stored as the exceptional result in the shared state of the resulting future.
Returns:
The return type of then depends on the return type of func as defined below:
  • When result_of_t<decay_t<F>(future<R>)> is future<R2>, for some type R2, the function returns future<R2>.
  • Otherwise, the function returns future<result_of_t<decay_t<F>(future<R>)>>.

    [ Note: The first rule above is called implicit unwrapping. Without this rule, the return type of then taking a callable returning a future<R> would have been future<future<R>>. This rule avoids such nested future objects. The type of f2 below is future<int> and not future<future<int>>: [ Example:
    future<int> f1 = g();
    future<int> f2 = f1.then([](future<int> f) {
                        future<int> f3 = h();
                        return f3;
                     });
    
    end example ]
    end note ]
Postconditions:
  • valid() == false on the original future. valid() == true on the future returned from then.
    Notes:
    In case of implicit unwrapping, the validity of the future returned from then cannot be established until after the completion of the continuation. If it is not valid, the resulting future becomes ready with an exception of type std::future_error, with an error condition of std::future_errc::broken_promise.
bool is_ready() const noexcept;
Returns:
true if the shared state is ready, otherwise false.
2.4

Class template shared_future

[futures.shared_future]

The specification of all declarations within this sub-clause 2.4 and its sub-clauses are the same as the corresponding declarations, as specified in C++14 §30.6.7, unless explicitly specified otherwise.

  namespace std {
  namespace experimental {
  inline namespace concurrency_v1 {

    template <class R>
    class shared_future {
    public:
      shared_future() noexcept;
      shared_future(const shared_future&) noexcept;
      shared_future(future<R>&&) noexcept;
      shared_future(future<shared_future<R>>&& rhs) noexcept;
      ~shared_future();
      shared_future& operator=(const shared_future&);
      shared_future& operator=(shared_future&&) noexcept;

      // retrieving the value
      see below get();

      // functions to check state
      bool valid() const noexcept;
      bool is_ready() const noexcept;

      void wait() const;
      template <class Rep, class Period>
        future_status wait_for(const chrono::duration<Rep, Period>& rel_time) const;
      template <class Clock, class Duration>
        future_status wait_until(const chrono::time_point<Clock, Duration>& abs_time) const;

      // continuations
      template <class F>
        see below then(F&& func) const;
    };

  } // namespace concurrency_v1
  } // namespace experimental
  } // namespace std

After C++14 §30.6.7 paragraph 10, add the following:

shared_future(future<shared_future<R>>&& rhs) noexcept;
Effects:
Constructs a shared_future object from the shared state referred to by rhs. The shared_future becomes ready when one of the following occurs:
  • Both the rhs and rhs.get() are ready. The value or the exception from rhs.get() is stored in the shared_future's shared state.
  • rhs is ready but rhs.get() is invalid. The shared_future stores an exception of type std::future_error, with an error condition of std::future_errc::broken_promise.
Postconditions:
  • valid() == true.
  • rhs.valid() == false.

After C++14 §30.6.7 paragraph 28, add the following:

The member function template then provides a mechanism for attaching a continuation to a shared_future object, which will be executed as specified below.


template <class F>
see below then(F&& func) const;
Requires:
INVOKE(DECAY_COPY (std::forward<F>(func)), *this) shall be a valid expression.
Effects:
The function creates a shared state that is associated with the returned future object. The further behavior of the function is defined below.
  • When the object's shared state is ready, the continuation INVOKE(DECAY_COPY(std::forward<F>(func)), *this) is called on an unspecified thread of execution with the call to DECAY_COPY() being evaluated in the thread that called then.
  • Any value returned from the continuation is stored as the result in the shared state of the resulting future. Any exception propagated from the execution of the continuation is stored as the exceptional result in the shared state of the resulting future.
Returns:
The return type of then depends on the return type of func as defined below:
  • When result_of_t<decay_t<F>(shared_future<R>)> is future<R2>, for some type R2, the function returns future<R2>.
  • Otherwise, the function returns future<result_of_t<decay_t<F>(shared_future<R>)>>.

    [ Note: This analogous to future. See the notes on future::then return type in 2.3. end note ]

Postconditions:
  • valid() == true on the original shared_future object. valid() == true on the future returned from then.
    Notes:
    In case of implicit unwrapping, the validity of the future returned from then cannot be established until after the completion of the continuation. In such case, the resulting future becomes ready with an exception of type std::future_error, with an error condition of std::future_errc::broken_promise.
bool is_ready() const noexcept;
Returns:
true if the shared state is ready, otherwise false.
2.5

Class template promise

[futures.promise]

The specification of all declarations within this sub-clause 2.5 and its sub-clauses are the same as the corresponding declarations, as specified in C++14 §30.6.5, unless explicitly specified otherwise.

The future returned by the function get_future is the one defined in the experimental namespace (2.3).

2.6

Class template packaged_task

[futures.task]

The specification of all declarations within this sub-clause 2.6 and its sub-clauses are the same as the corresponding declarations, as specified in C++14 §30.6.9, unless explicitly specified otherwise.

The future returned by the function get_future is the one defined in the experimental namespace (2.3).

2.7

Function template when_all

[futures.when_all]

The function template when_all creates a future object that becomes ready when all elements in a set of future and shared_future objects become ready.


template <class InputIterator>
future<vector<typename iterator_traits<InputIterator>::value_type>>
when_all(InputIterator first, InputIterator last);

template <class... Futures>
future<tuple<decay_t<Futures>...>> when_all(Futures&&... futures);
Requires:
  • For the first overload, iterator_traits<InputIterator>::value_type must be future<R> or shared_future<R>, for some type R.
  • All futures and shared_futures passed into when_all must be in a valid state (i.e. valid() == true).
Notes:
  • Calling the first overload of when_all where first == last, returns a future with an empty vector that is immediately ready.
  • Calling the second overload of when_all with no arguments returns a future<tuple<>> that is immediately ready.
Remarks:
For the second overload, let Ui be decay_t<Fi> for each Fi in Futures. This function shall not participate in overload resolution unless each Ui is either future<Ri> or shared_future<Ri>.
Effects:
  • A new shared state containing a Sequence is created, where Sequence is either tuple or a vector based on the overload, as specified above. A new future object that refers to that shared state is created and returned from when_all.
  • Once all the futures and shared_futures supplied to the call to when_all are ready, the futures are moved, and the shared_futures are copied, into, correspondingly, futures or shared_futures of the futures member of Sequence in the shared state. The order of the objects in the shared state matches the order of the arguments supplied to when_all.
  • The future returned by when_all will not throw an exception, but the futures and shared_futures held in the shared state may.
Postconditions:
  • valid() == true.
  • For all input future<T>s, valid() == false.
  • For all input shared_future<T>s, valid() == true.
2.8

Class template when_any_result

[futures.when_any_result]

The library provides a template for storing the result of when_any.


template<class Sequence>
struct when_any_result {
    size_t index;
    Sequence futures;
};
2.9

Function template when_any

[futures.when_any]

The function template when_any creates a future object that becomes ready when at least one element in a set of future and shared_future objects becomes ready.


template <class InputIterator>
future<when_any_result<vector<typename iterator_traits<InputIterator>::value_type>>>
when_any(InputIterator first, InputIterator last);

template <class... Futures>
future<when_any_result<tuple<decay_t<Futures>...>>> when_any(Futures&&... futures);
Requires:
  • For the first overload, iterator_traits<InputIterator>::value_type must be future<R> or shared_future<R>, for some type R.
  • All futures and shared_futures passed into when_any must be in a valid state (i.e. valid() == true).
Notes:
  • Calling the first overload of when_any where first == last, returns a future that is immediately ready. The value of the index field of the when_any_result is unspecified. The futures field is an empty vector.
  • Calling the second overload of when_any with no arguments returns a future that is immediately ready. The value of the index field of the when_any_result is unspecified. The futures field is tuple<>.
Remarks:
For the second overload, let Ui be decay_t<Fi> for each Fi in Futures. This function shall not participate in overload resolution unless each Ui is either future<Ri> or shared_future<Ri>.
Effects:
  • A new shared state containing when_any_result<Sequence> is created, where Sequence is a vector for the first overload and a tuple for the second overload. A new future object that refers to that shared state is created and returned from when_any.
  • Once at least one of the futures or shared_futures supplied to the call to when_any is ready, the futures are moved, and the shared_futures are copied into, correspondingly, futures or shared_futures of the futures member of Sequence in the shared state.
  • The order of the objects in the futures shared state matches the order of the arguments supplied to when_any.
  • The future returned by when_any will not throw an exception, but the futures and shared_futures held in the shared state may.
Postconditions:
  • valid() == true.
  • For all input future<T>s, valid() == false.
  • For all inputshared_future<T>s, valid() == true.
Returns:
  • A future object that becomes ready when any of the input futures/shared_futures are ready.
2.10

Function template make_ready_future

[futures.make_ready_future]

A new section 30.6.13 shall be inserted at the end of C++14 §30.6. Below is the content of that section.


template <class T>
future<V> make_ready_future(T&& value);

future<void> make_ready_future();
  

Let U be decay_t<T>. Then V is X& if U equals reference_wrapper<X>, otherwise V is U.

Effects:
  • For the first overload, the value that is passed in to the function is moved to the shared state of the returned future if it is an rvalue. Otherwise the value is copied to the shared state of the returned future.
  • The second overload creates a shared state for the returned future.
The value that is passed in to the function is moved to the shared state of the returned future if it is an rvalue. Otherwise the value is copied to the shared state of the returned future.
Returns:
  • future<V>, if function is given a value of type T.
  • future<void>, if the function is not given any inputs.
Postconditions:
  • Returned future<V>, valid() == true.
  • Returned future<V>, is_ready() == true.
2.11

Function template make_exceptional_future

[futures.make_exceptional_future]

A new section 30.6.13 shall be inserted at the end of C++14 §30.6. Below is the content of that section.


template <class T>
future<T> make_exceptional_future(exception_ptr ex);
Effects:
Equivalent to
promise<T> p;
p.set_exception(ex);
return p.get_future();
template <class T, class E>
future<T> make_exceptional_future(E ex);
Effects:
Equivalent to
promise<T> p;
p.set_exception(make_exception_ptr(ex));
return p.get_future();
3

Latches and Barriers

[coordination]
3.1

General

[coordination.general]

This section describes various concepts related to thread co-ordination, and defines the latch, barrier and flex_barrier classes.

3.2

Terminology

[thread.coordination.terminology]

In this sub-clause, a synchronization point represents a point at which a thread may block until a given condition has been reached.

3.3

Latches

[thread.coordination.latch]

Latches are a thread coordination mechanism that allow one or more threads to block until an operation is completed. An individual latch is a single-use object; once the operation has been completed, the latch cannot be reused.

3.4

Header <experimental/latch> synopsis

[thread.coordination.latch.synopsis]

namespace std {
namespace experimental {
inline namespace concurrency_v1 {
  class latch {
   public:
    explicit latch(ptrdiff_t count);
    latch(const latch&) = delete;
    latch(latch&&) = delete;
    
    ~latch();


    latch& operator=(const latch&) = delete;
    latch& operator=(latch&&) = delete;

    void count_down_and_wait();
    void count_down(ptrdiff_t n);


    bool is_ready() const noexcept;
    void wait() const;

   private:
    ptrdiff_t counter_; // exposition only
  };
} // namespace concurrency_v1
} // namespace experimental
} // namespace std
3.5

Class latch

[coordination.latch.class]

A latch maintains an internal counter_ that is initialized when the latch is created. Threads may block at a synchronization point waiting for counter_ to be decremented to 0. When counter_ reaches 0, all such blocked threads are released.

Calls to countdown_and_wait(), count_down(), wait(), and is_ready() behave as atomic operations.

explicit latch(ptrdiff_t count);
Requires:
count >= 0.
Synchronization:
None
Postconditions:
counter_ == count.
~latch();
Requires:
No threads are blocked at the synchronization point.
Remarks:
May be called even if some threads have not yet returned from wait() or count_down_and_wait() provided that counter_ is 0. [ Note: The destructor might not return until all threads have exited wait() or count_down_and_wait(). end note ]
void count_down_and_wait();
Requires:
counter_ > 0.
Effects:
Decrements counter_ by 1. Blocks at the synchronization point until counter_ reaches 0.
Synchronization:
Synchronizes with all calls that block on this latch and with all is_ready calls on this latch that return true.
Throws:
Nothing.
void count_down(ptrdiff_t n);
Requires:
counter_ >= n and n >= 0.
Effects:
Decrements counter_ by n. Does not block.
Synchronization:
Synchronizes with all calls that block on this latch and with all is_ready calls on this latch that return true.
Throws:
Nothing.
void wait() const;
Effects:
If counter_ is 0, returns immediately. Otherwise, blocks the calling thread at the synchronization point until counter_ reaches 0.
Throws:
Nothing.
is_ready() const noexcept;
Returns:
counter_ == 0. Does not block.
3.6

Barrier types

[thread.coordination.barrier]

Barriers are a thread coordination mechanism that allow a set of participating threads to block until an operation is completed. Unlike a latch, a barrier is re-usable: once the participating threads are released from a barrier's synchronization point, they can re-use the same barrier. It is thus useful for managing repeated tasks, or phases of a larger task, that are handled by multiple threads.

The barrier types are the standard library types barrier and flex_barrier. They shall meet the requirements set out in this sub-clause. In this description, b denotes an object of a barrier type.

Each barrier type defines a completion phase as a (possibly empty) set of effects. When the member functions defined in this sub-clause arrive at the barrier's synchronization point, they have the following effects:

  1. The function blocks.
  2. When all threads in the barrier's set of participating threads are blocked at its synchronization point, one participating thread is unblocked and executes the barrier type's completion phase.
  3. When the completion phase is completed, all other participating threads are unblocked. The end of the completion phase synchronizes with the returns from all calls unblocked by its completion.

The expression b.arrive_and_wait() shall be well-formed and have the following semantics:

void arrive_and_wait();
Requires:
The current thread is a member of the set of participating threads.
Effects:
Arrives at the barrier's synchronization point. [ Note: It is safe for a thread to call arrive_and_wait() or arrive_and_drop() again immediately. It is not necessary to ensure that all blocked threads have exited arrive_and_wait() before one thread calls it again. end note ]
Synchronization:
The call to arrive_and_wait() synchronizes with the start of the completion phase.
Throws:
Nothing.

The expression b.arrive_and_drop() shall be well-formed and have the following semantics:

void arrive_and_drop();
Requires:
The current thread is a member of the set of participating threads.
Effects:
Either arrives at the barrier's synchronization point and then removes the current thread from the set of participating threads, or just removes the current thread from the set of participating threads. [ Note: Removing the current thread from the set of participating threads can cause the completion phase to start. end note ]
Synchronization:
The call to arrive_and_drop() synchronizes with the start of the completion phase.
Throws:
Nothing.
Notes:
If all participating threads call arrive_and_drop(), any further operations on the barrier are undefined, apart from calling the destructor. If a thread that has called arrive_and_drop() calls another method on the same barrier, other than the destructor, the results are undefined.

Calls to arrive_and_wait() and arrive_and_drop() never introduce data races with themselves or each other.

3.7

Header <experimental/barrier> synopsis

[thread.coordination.barrier.synopsis]

namespace std {
namespace experimental {
inline namespace concurrency_v1 {
  class barrier;
  class flex_barrier;
} // namespace concurrency_v1
} // namespace experimental
} // namespace std
3.8

Class barrier

[coordination.barrier.class]

barrier is a barrier type whose completion phase has no effects. Its constructor takes a parameter representing the initial size of its set of participating threads.


class barrier {
 public:
  explicit barrier(ptrdiff_t num_threads);
  barrier(const barrier&) = delete;
  barrier(barrier&&) = delete;

  ~barrier();


  barrier& operator=(const barrier&) = delete;
  barrier& operator=(barrier&&) = delete;

  void arrive_and_wait();
  void arrive_and_drop();
};
explicit barrier(ptrdiff_t num_threads);
Requires:
num_threads >= 0. [ Note: If num_threads is zero, the barrier may only be destroyed. end note ]
Effects:
Initializes the barrier for num_threads participating threads. [ Note: The set of participating threads is the first num_threads threads to arrive at the synchronization point. end note ]
~barrier();
Requires:
No threads are blocked at the synchronization point.
Effects:
Destroys the barrier
3.9

Class flex_barrier

[coordination.flexbarrier.class]

flex_barrier is a barrier type whose completion phase can be controlled by a constructor parameter.


class flex_barrier {
 public:
  template <class F>
    flex_barrier(ptrdiff_t num_threads, F completion);
  explicit flex_barrier(ptrdiff_t num_threads);
  flex_barrier(const flex_barrier&) = delete;
  flex_barrier(flex_barrier&&) = delete;

  ~flex_barrier();


  flex_barrier& operator=(const flex_barrier&) = delete;
  flex_barrier& operator=(flex_barrier&&) = delete;

  void arrive_and_wait();
  void arrive_and_drop();


 private:
  function<ptrdiff_t()> completion_;  // exposition only
};

The completion phase calls completion_(). If this returns -1, then the set of participating threads is unchanged. Otherwise, the set of participating threads becomes a new set with a size equal to the returned value. [ Note: If completion_() returns 0 then the set of participating threads becomes empty, and this object may only be destroyed. end note ]


template <class F>
flex_barrier(ptrdiff_t num_threads, F completion);
  
Requires:
  • num_threads >= 0.
  • F shall meet the requirements of CopyConstructible.
  • completion shall be Callable (C++14 §[func.wrap.func]) with no arguments and return type convertible to ptrdiff_t.
  • Invoking completion shall return a value greater than or equal to -1 and shall not exit via an exception.
Effects:
Initializes the flex_barrier with the set of participating threads, of size num_threads, and initializes completion_ with std::move(completion). [ Note: The set of participating threads consists of the first num_threads threads that will arrive at the synchronization point. end note ]
Notes:
If num_threads is zero the set of participating threads is empty, and this object may only be destroyed.
explicit flex_barrier(ptrdiff_t num_threads);
Requires:
num_threads >= 0.
Effects:
Has the same effect as creating a flex_barrier with num_threads and with a callable object whose invocation returns -1 and has no side effects.
~flex_barrier();
Requires:
No threads are blocked at the synchronization point.
Effects:
Destroys the barrier.
4

Atomic Smart Pointers

[atomic]
4.1

General

[atomic.smartptr.general]

This section provides alternatives to raw pointers for thread-safe atomic pointer operations, and defines the atomic_shared_ptr and atomic_weak_ptr classes.

The class templates atomic_shared_ptr<T> and atomic_weak_ptr<T> have the corresponding non-atomic types shared_ptr<T> and weak_ptr<T>. The template parameter T of these class templates may be an incomplete type.

The behavior of all operations is as specified in C++14 §29.6.5, unless stated otherwise.

4.2

Header <experimental/atomic> synopsis

[atomic.smartptr.synop]

#include <memory>

template <class T> struct atomic_shared_ptr;
template <class T> struct atomic_weak_ptr;
4.3

Class template atomic_shared_ptr

[atomic.shared_ptr]

namespace std {
  namespace experimental {
  inline namespace concurrency_v1 {

  template <class T> struct atomic_shared_ptr {
    bool is_lock_free() const noexcept;
    void store(shared_ptr<T>, memory_order = memory_order_seq_cst) noexcept;
    shared_ptr<T> load(memory_order = memory_order_seq_cst) const noexcept;
    operator shared_ptr<T>() const noexcept;
    
    shared_ptr<T> exchange(shared_ptr<T>, 
      memory_order = memory_order_seq_cst) noexcept;
    
    bool compare_exchange_weak(shared_ptr<T>&, const shared_ptr<T>&,
      memory_order, memory_order) noexcept;
    bool compare_exchange_weak(shared_ptr<T>&, shared_ptr<T>&&, 
      memory_order,  memory_order) noexcept;
    bool compare_exchange_weak(shared_ptr<T>&, const shared_ptr<T>&,
      memory_order = memory_order_seq_cst) noexcept;
    bool compare_exchange_weak(shared_ptr<T>&, shared_ptr<T>&&, 
      memory_order = memory_order_seq_cst) noexcept;

    bool compare_exchange_strong(shared_ptr<T>&, const shared_ptr<T>&,
      memory_order, memory_order) noexcept;
    bool compare_exchange_strong(shared_ptr<T>&, shared_ptr<T>&&,
      memory_order, memory_order) noexcept;
    bool compare_exchange_strong(shared_ptr<T>&, const shared_ptr<T>&,
      memory_order = memory_order_seq_cst) noexcept;
    bool compare_exchange_strong(shared_ptr<T>&, shared_ptr<T>&&, 
      memory_order = memory_order_seq_cst) noexcept;

    atomic_shared_ptr() noexcept = default;
    constexpr atomic_shared_ptr(shared_ptr<T>) noexcept;
    atomic_shared_ptr(const atomic_shared_ptr&) = delete;
    atomic_shared_ptr& operator=(const atomic_shared_ptr&) = delete;
    atomic_shared_ptr& operator=(shared_ptr<T>) noexcept;
  };
  } // namespace concurrency_v1
  } // namespace experimental
} // namespace std

atomic_shared_ptr::atomic_shared_ptr() noexcept = default;
Effects:
Initializes the atomic object to an empty value.
4.4

Class template atomic_weak_ptr

[atomic.weak_ptr]

namespace std {
  namespace experimental {
  inline namespace concurrency_v1 {

  template <class T> struct atomic_weak_ptr {
    bool is_lock_free() const noexcept;

    void store(weak_ptr<T>, memory_order = memory_order_seq_cst) noexcept;
    weak_ptr<T> load(memory_order = memory_order_seq_cst) const noexcept;
      operator weak_ptr<T>() const noexcept;
    weak_ptr<T> exchange(weak_ptr<T>, memory_order = memory_order_seq_cst) noexcept;

    bool compare_exchange_weak(weak_ptr<T>&, const weak_ptr<T>&,
      memory_order, memory_order) noexcept;
    bool compare_exchange_weak(weak_ptr<T>&, weak_ptr<T>&&,
      memory_order, memory_order) noexcept;
    bool compare_exchange_weak(weak_ptr<T>&, const weak_ptr<T>&, 
      memory_order = memory_order_seq_cst) noexcept;
    bool compare_exchange_weak(weak_ptr<T>&, weak_ptr<T>&&, 
      memory_order = memory_order_seq_cst) noexcept;

    bool compare_exchange_strong(weak_ptr<T>&, const weak_ptr<T>&, 
      memory_order, memory_order) noexcept;
    bool compare_exchange_strong(weak_ptr<T>&, weak_ptr<T>&&, 
      memory_order, memory_order) noexcept;
    bool compare_exchange_strong(weak_ptr<T>&, const weak_ptr<T>&, 
      memory_order = memory_order_seq_cst) noexcept;
    bool compare_exchange_strong(weak_ptr<T>&, weak_ptr<T>&&, 
      memory_order = memory_order_seq_cst) noexcept;

    atomic_weak_ptr() noexcept = default;
    constexpr atomic_weak_ptr(weak_ptr<T>) noexcept;
    atomic_weak_ptr(const atomic_weak_ptr&) = delete;
    atomic_weak_ptr& operator=(const atomic_weak_ptr&) = delete;
    atomic_weak_ptr& operator=(weak_ptr<T>) noexcept;
  };
  } // namespace concurrency_v1
  } // namespace experimental
} // namespace std

atomic_weak_ptr::atomic_weak_ptr() noexcept = default;
Effects:
Initializes the atomic object to an empty value.

When any operation on an atomic_shared_ptr or atomic_weak_ptr causes an object to be destroyed or memory to be deallocated, that destruction or deallocation shall be sequenced after the changes to the atomic object's state.

[ Note: This prevents potential deadlock if the atomic smart pointer operation is not lock-free, such as by including a spinlock as part of the atomic object's state, and the destruction or the deallocation may attempt to acquire a lock. end note ]

[ Note: These types replace all known uses of the functions in C++14 §20.8.2.6. end note ]