std::jthrea..._stop_token(3)

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

🌸 NAME 🌸

okay so bestie, this is the manual page for std::jthread::get_stop_token, straight out of the C++ Standard Libary (that's the manual name near the top β€” spelling is their business, not mine πŸ’…). The function is literally std::jthread::get_stop_token β€” yes, same name twice, we're not doing a sequel. 🫢


✨ Synopsis ✨

std::stop_token get_stop_token() const noexcept;  (since C++20)

It returns a std::stop_token that's associated with the same shared stop-state held internally by the jthread object. That's the whole vibe. πŸ¦‹


πŸŽ€ Parameters πŸŽ€

(none) β€” bestie, there are literally none. No parameters, no secret bonus args, nada. ✨


πŸ’– Return value πŸ’–

A value of type std::stop_token associated with the stop-state held internally by the jthread object. It's giving exactly what the synopsis promised. πŸ‘‘


πŸ¦‹ Example πŸ¦‹

okay, code time. Run this code, bestie:

        // Run this code

        #include <chrono>
        #include <condition_variable>
        #include <iostream>
        #include <mutex>
        #include <string_view>
        #include <thread>

        using namespace std::chrono_literals;

        void print(std::string_view name, const std::stop_token& token)
        {
            std::cout << name << ": stop_possible = " << token.stop_possible();
            std::cout << ", stop_requested = " << token.stop_requested() <<
       '\n';
        }

        void finite_sleepy(std::stop_token stoken)
        {
            for (int i = 10; i; --i)
            {
                std::this_thread::sleep_for(300ms);
                if (stoken.stop_requested())
                {
                    std::cout << "  Sleepy worker is requested to stop\n";
                    return;
                }

                std::cout << "  Sleepy worker goes back to sleep\n";
            }
        }

        void infinite_sleepy()
        {
            for (int i = 5; i; --i)
            {
                std::this_thread::sleep_for(300ms);
                std::cout << "  Run as long as we want\n";
            }
        }

        int main()
        {
            std::cout << std::boolalpha;

            // A worker thread that will listen to stop requests
            std::jthread stop_worker(finite_sleepy);

            // A worker thread that will only stop when completed
            std::jthread inf_worker(infinite_sleepy);

            std::stop_token def_token;
            std::stop_token stop_token = stop_worker.get_stop_token();
            std::stop_token inf_token = inf_worker.get_stop_token();
            print("def_token ", def_token);
            print("stop_token", stop_token);
            print("inf_token ", inf_token);

            std::cout << "\nRequest and join stop_worker:\n";
            stop_worker.request_stop();
            stop_worker.join();

            std::cout << "\nRequest and join inf_worker:\n";
            inf_worker.request_stop();
            inf_worker.join();
            std::cout << '\n';

            print("def_token ", def_token);
            print("stop_token", stop_token);
            print("inf_token ", inf_token);
        }

🌈 Possible output 🌈

        def_token : stop_possible = false, stop_requested = false
        stop_token: stop_possible = true, stop_requested = false
        inf_token : stop_possible = true, stop_requested = false

        Request and join stop_worker:
          Run as long as we want
          Sleepy worker is requested to stop

        Request and join inf_worker:
          Run as long as we want
          Run as long as we want
          Run as long as we want
          Run as long as we want

        def_token : stop_possible = false, stop_requested = false
        stop_token: stop_possible = true, stop_requested = true
        inf_token : stop_possible = true, stop_requested = true

mwah πŸ’‹ this page came from http://cppreference.com, dated 2024.06.10 β€” std::jthrea..._stop_token(3), periodt. πŸŽ€

πŸ“¦ where this page comes from