mardi 1 septembre 2015

std::shared_future operator= thread safety/ atomic?

General question: Is std::shared_future::operator= atomic?

For example

struct object {
    object() {
        sf = std::async(std::launch::async, &async_func).share(); 
    }
    void change(){
        sf = std::async(std::launch::async, &other_async_func).share();
    }
    void read(){
        while (true){ sf.get(); }
    }
    std::shared_future<int> sf;
};

Question Part 1 Is it ok to call std::shared_future::operator= while the left e.g. old shared_future has not been waited on/asynchronous provider still running? Like in object::change().

Question Part 2 Is it ok to call std::shared_future::operator= while other asynchronous return objects / threads that are concurrent calling std::shared_future.get()? Like in object::read()?

After reading of C++11 draft N3485 §30.6.7:12

shared_future& operator=(shared_future&& rhs) noexcept; 12 Effects:

— releases any shared state (30.6.4);

— move assigns the contents of rhs to *this

Question Part 1 depends solely on releasing a shared state e.g. after reading of §30.6.4, destroying a shared state so I guess that means, Part 1 should be true, but I'm not sure.

Question Part 2 seems to be false, because this are 2 steps and I neither don't know if the move part is atomic nor if what happens if the shared state is destroyed while other threads are in shared_future::get()

Aucun commentaire:

Enregistrer un commentaire