Quantcast
Channel: Recent Questions - Stack Overflow
Viewing all articles
Browse latest Browse all 15970

std::jtrhead doesn't seem to invoke class member function

$
0
0

I'm struggling to re-learn C++ and I'm playing around with jthread at the moment. I seem to have got things working when calling a class member function that I want on its own thread. However, the function never actually seems to get called. I can't work out what I'm missing here. If someone can point out my inevitably "learner's" error, I'd be really grateful.

// includes and directives#include <iostream>#include <thread>#include <functional>#include <chrono>// a class for testing stuffclass A {private:    int updates{ 0 }; // something to count on    int tick{ 1000 }; // tick time set to 1000ms initiallypublic:    void set_tick(int t) {        tick = (t<1?1:t); // sanity check and set tick time    }    int get_updates() {        return updates; // I did how many?    }    void update(std::stop_token st) {        while (!st.stop_requested()) { // stop if they say stop            updates++; // count this as an update            std::cout << "Performing update" << updates << "\n"; // visually indicate something happened            std::this_thread::sleep_for(std::chrono::milliseconds(tick)); // wait for tick time before doing it all again        }    }    void start() { // start a ticking function        std::cout << "Starting update thread\n"; // tell someone responsible that it's started        std::jthread thr_A(std::bind_front(&A::update, this)); // start the parallel process fucntion in this class    }};// main thingint main(){    std::cout << "Start main thread\n"; // say that I'm starting up     A a; // make me a sandwich    a.set_tick(10); // really fast(ish)    a.start(); // go on, get on with it    std::this_thread::sleep_for(std::chrono::milliseconds(10000)); // wait until sandwich ready    std::cout << "Wake main thread and terminate\n"; // sandwich is ready, time to eat}

Viewing all articles
Browse latest Browse all 15970

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>