mercredi 1 mars 2017

Is std::promise

Is it safe, like in the case of std::mutex for a std::promise<T> to be made mutable, or does it depend on T? As in:

using Data = std::tuple<bool, int, int>;

struct X {

    std::future<Data> prepare() const {
        return m_promise.get_future();
    }

    void asyncHandler(int a, int b) const {
        m_promise.set_value({true, a, b});
    }

    void cancel() const {
        m_promise.set_value({false, 0, 0});
    }

    mutable std::promise<Data> m_promise;  // Is this safe?
};


void performAsyncOp(const X& x) {
     std::future<Data> fut = x.prepare();
     dispatch(x);
     std::future_status result = fut.wait_for(std::chrono::milliseconds(150));
     if (result == std::future_status::timeout) {
         x.cancel();
     }

     handleResult(fut.get());
}

Aucun commentaire:

Enregistrer un commentaire