Namespaces
Variants

std::execution::set_value

From cppreference.com
 
 
Execution control library
Concepts
 
Defined in header <execution>
struct set_value_t { /* unspecified */ };
(1) (since C++26)
inline constexpr set_value_t set_value{};
(2) (since C++26)
(customization point object)
Call signature
template< class Rcvr, class... Vs >
void set_value( Rcvr&& rcvr, Vs&&... vals ) noexcept;
(3) (since C++26)

The value completion function: set_value(std::move(rcvr), vals...) tells the receiver rcvr that the asynchronous operation it was connected to has succeeded, and passes it the results of the operation, vals.... There can be any number of values, including none: set_value(std::move(rcvr)) reports success without a result, as the sender returned by schedule does.

An operation completes by calling exactly one of the three completion functions on its receiver: set_value on success, set_error on failure, or set_stopped when it was stopped before it could finish. The sender algorithms and the receivers of the standard library call them; a program calls set_value itself when it writes its own sender, in the operation state that sender returns from connect.

1) set_value_t is the completion tag of value completions. It is used to name them, for example:
  • in completion signatures: set_value_t(int, std::string) means that a sender can complete with set_value(std::move(rcvr), i, s) where i is an int and s is a std::string; see completion_signatures;
  • in queries: get_completion_scheduler<set_value_t> asks where a sender completes on success; see get_completion_scheduler.
3) The expression set_value(rcvr, vals...) is expression-equivalent to rcvr.set_value(vals...). It is ill-formed if rcvr is an lvalue or a const rvalue: a receiver is passed as a non-const rvalue, usually with std::move, because completing uses it up. The program is ill-formed if the expression rcvr.set_value(vals...) is well-formed but can throw an exception, or has a type other than void.

Parameters

rcvr - the receiver of the operation that completes
vals - the results of the operation

Customization point objects

The name execution::set_value denotes a customization point object, which is a const function object of a literal semiregular class type. The cv-unqualified version of its type is a tag type denoted as execution::set_value_t. See CustomizationPointObject for details.

Notes

  • A receiver accepts a value completion by providing a noexcept member function set_value that returns void and can be called with the values; see receiver. It may be declared &&. A receiver can overload it to accept several kinds of value completions.
  • Completing cannot throw, because there is nowhere for the exception to go. If computing the values can throw, compute them first, and if that throws, complete with set_error(std::move(rcvr), std::current_exception()) instead; this is what then does if its function throws. The conversions of the values to the parameter types of the receiver's set_value are part of the call, so they must not throw either. The current wording also requires the evaluation of the arguments themselves not to throw, which is the subject of LWG issue 4353.
  • The values a sender can complete with are part of its completion signatures, so that the code that uses the sender knows them at compile time. An operation must only call set_value with values that match one of the set_value_t signatures of its sender, after the operation was started, and only once.
  • The operation state can be destroyed while set_value is still running, as soon as the receiver has what it needs: for example, this_thread::sync_wait can return and destroy it from another thread. The call to set_value should therefore be the last thing the operation does with its own members.
  • The completion can happen on any thread: on the one that called start, if the operation completes immediately, or on the thread on which the work finished. What the receiver does next, such as the function passed to then, runs on that thread.

Example

square_sender wraps an API that reports its result to a callback on its own thread: the callback completes the operation with set_value, and the function passed to then then runs on that thread.
The execution control library components may not be available in the online compilers yet, so this link runs the example on Compiler Explorer with stdexec (one of the reference implementations).

#include <exception>
#include <execution>
#include <iostream>
#include <thread>
#include <utility>

namespace ex = std::execution;

// A callback-based API: computes x * x on its own thread and passes the
// result to the callback
struct legacy_service
{
    std::jthread worker;

    void async_square(int x, auto callback)
    {
        worker = std::jthread([=] { callback(x * x); });
    }
};

// A sender that wraps the callback-based API
struct square_sender
{
    using sender_concept = ex::sender_tag;
    legacy_service* service;
    int x;

    template<class Self, class... Env>
    static consteval auto get_completion_signatures()
    {
        return ex::completion_signatures<ex::set_value_t(int),
                                         ex::set_error_t(std::exception_ptr)>();
    }

    template<class Rcvr>
    struct operation
    {
        using operation_state_concept = ex::operation_state_tag;
        Rcvr rcvr;
        legacy_service* service;
        int x;

        void start() & noexcept
        {
            try
            {
                service->async_square(x, [this](int result)
                {
                    // The last use of *this: the operation state can be
                    // destroyed as soon as the receiver has the value
                    ex::set_value(std::move(rcvr), result);
                });
            }
            catch (...)
            {
                ex::set_error(std::move(rcvr), std::current_exception());
            }
        }
    };

    template<class Rcvr>
    operation<Rcvr> connect(Rcvr rcvr) const
    {
        return {std::move(rcvr), service, x};
    }
};

int main()
{
    legacy_service service;
    auto work = square_sender{&service, 7} | ex::then([](int value)
    {
        return std::pair(value + 1, std::this_thread::get_id());
    });
    auto [result] = std::this_thread::sync_wait(std::move(work)).value();

    std::cout << std::boolalpha << "Result: " << result.first << '\n'
              << "Ran on the main thread: "
              << (result.second == std::this_thread::get_id()) << '\n';
}

Output:

Result: 50
Ran on the main thread: false

See also

error completion function indicating that an error occurred during calculation or scheduling
(customization point object)[edit]
stopped completion function indicating that an operation ended before it could achieve success or failure
(customization point object)[edit]
specifies that a type is a receiver
(concept) [edit]
lists the ways a sender can complete
(class template) [edit]
returns a sender that immediately sends copies of the given values
(customization point object)[edit]