std::promise::set_value(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

πŸ’– std::promise::set_value(3) β€” C++ Standard Libary, bestie edition πŸ’–

okay so this is the std::promise::set_value page from the C++ Standard Libary, and it's literally the glow-up your threads needed. No cap. 🫢


🌸 NAME 🌸

std::promise::set_value - std::promise::set_value

(Yes, the name echoes itself. Iconic behavior. We love a self-aware queen. πŸ‘‘)


✨ Synopsis ✨

Main template
void set_value( const R& value ); (1) (since C++11)
void set_value( R&& value );      (2) (since C++11)
std::promise<R&> specializations
void set_value( R& value );       (3) (since C++11)
std::promise<void> specialization
void set_value();                 (4) (since C++11)
  • 1-3) Atomically stores value into the shared state and makes the state ready. Slay. πŸ’…
  • 4) Makes the state ready.

The operation behaves as though set_value, set_exception, set_value_at_thread_exit, and set_exception_at_thread_exit acquire a single mutex associated with the promise object while updating the promise object.

Calls to this function do not introduce data races with calls to get_future (therefore they need not synchronize with each other).


πŸŽ€ Parameters πŸŽ€

  • value β€” value to store in the shared state. That's the whole assignment, bestie. πŸ“

🫢 Return value 🫢

(none) β€” literally nothing comes back. It just does the thing and leaves. Main character energy. ✨


😭 Exceptions 😭

std::future_error on the following conditions:

  • *this has no shared state. The error code is set to no_state.
  • The shared state already stores a value or exception. The error code is set to promise_already_satisfied.

Additionally:

  1. Any exception thrown by the constructor selected to copy an object of type R.
  2. Any exception thrown by the constructor selected to move an object of type R.
🫢 Not me warning you that this can throw std::future_error β€” protect your shared state, bestie. No cap.

πŸ’» Example πŸ’»

This example shows how std::promise<void> can be used as signals between threads.

There's even a // Run this code marker, so go off:

#include <algorithm>
#include <cctype>
#include <chrono>
#include <future>
#include <iostream>
#include <iterator>
#include <sstream>
#include <thread>
#include <vector>

using namespace std::chrono_literals;

int main()
{
    std::istringstream iss_numbers{"3 4 1 42 23 -23 93 2 -289 93"};
    std::istringstream iss_letters{" a 23 b,e a2 k k?a;si,ksa c"};

    std::vector<int> numbers;
    std::vector<char> letters;
    std::promise<void> numbers_promise, letters_promise;

    auto numbers_ready = numbers_promise.get_future();
    auto letter_ready = letters_promise.get_future();

    std::thread value_reader([&]
    {
        // I/O operations
        std::copy(std::istream_iterator<int>{iss_numbers},
                  std::istream_iterator<int>{},
                  std::back_inserter(numbers));

        // notify for numbers
        numbers_promise.set_value();

        std::copy_if(std::istreambuf_iterator<char>{iss_letters},
                     std::istreambuf_iterator<char>{},
                     std::back_inserter(letters),
                     ::isalpha);

        // notify for letters
        letters_promise.set_value();
    });

    numbers_ready.wait();

    std::sort(numbers.begin(), numbers.end());

    if (letter_ready.wait_for(1s) == std::future_status::timeout)
    {
        // output the numbers while letters are being obtained
        for (int num : numbers)
            std::cout << num << ' ';
        numbers.clear(); // numbers were already printed
    }

    letter_ready.wait();
    std::sort(letters.begin(), letters.end());

    // does nothing if numbers were already printed
    for (int num : numbers)
        std::cout << num << ' ';
    std::cout << '\n';

    for (char let : letters)
        std::cout << let << ' ';
    std::cout << '\n';

    value_reader.join();
}

Output:

-289 -23 1 2 3 4 23 42 93 93
a a a a b c e i k k k s s

πŸ” Defect reports πŸ”

The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

  • DR: LWG 2098 β€” Applied to: C++11 β€” Behavior as published: overloads (1,2) could only throw the exceptions thrown by the copy/move constructor of R respectively β€” Correct behavior: they can throw the exceptions thrown by the actual constructor selected to copy/move an object of type R.

πŸ‘€ See also πŸ‘€

  • set_value_at_thread_exit β€” sets the result to specific value while delivering the notification only at thread exit (public member function)
  • set_exception β€” sets the result to indicate an exception (public member function)
πŸ¦‹ That's the whole page, bestie! Source/version/date vibes: http://cppreference.com, 2024.06.10, and the page is std::promise::set_value(3). Periodt. πŸ’…
πŸ“¦ where this page comes from