dimanche 26 novembre 2017

Chained "fire-and-forget" waiting methods

I'm having a function that waits for certain amount of time before being proceed or it's successful run is dependent on other factors such as a boolean flag.

I want to chain callbacks to sequence the operations and wait for each one for successful run.

This is an example of how I want it to work:

int64_t m_runTime = 0;
bool clean = false;

void check() {
    if (time(nullptr) > m_runTime) {
        return; // not yet
    }

    m_runTime = time(nullptr) + 10;
    // Emit some kind of signal to .then saying that the 'check' has finished?
}

void cleanup() {
    // cleanup everything and emit signal

    if (!clean) {
        return; // not yet
    }

    [..]
    // ok, cleanup finished
}

check().then([]() {
        // check() finished, run cleanup
        cleanup();
    }).then([](){
        // cleanup() finished, output a message
        std::cout << "all task finished successfully!" << std:endl;
    });

Looks pretty much like a std::future job.

I did a little bit of research and found a nice method that (I guess) would satisfy my needs. However, it's not yet implemented. It does exist in the boost however, but I don't want to use any frameworks.

How can I implement this without std::future::then?

Aucun commentaire:

Enregistrer un commentaire