简体   繁体   English

std :: shared_future运算符=线程安全/原子?

[英]std::shared_future operator= thread safety/ atomic?

General question: Is std::shared_future::operator= atomic? 一般问题: std :: shared_future :: operator =原子吗?

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, eg old shared_future , has not been waited on/ asynchronous provider still running? 问题第1部分是否可以调用std::shared_future::operator=而左侧(例如旧的shared_future尚未等待/ 异步提供程序仍在运行? Like in object::change() . 就像在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() ? 问题第二部分 ,可以同时调用std::shared_future.get()其他异步返回对象 /线程来调用std::shared_future::operator=吗? Like in object::read() ? 像在object::read() Edit: Forget object::read() , I mean of course with their own std::shared_future but the same shared state . 编辑:忘了object::read() ,我的意思是当然有他们自己的std::shared_future但具有相同的共享状态

After reading of C++11 draft N3485 §30.6.7:12 读C ++ 11草案后N3485 §30.6.7:12

shared_future& operator=(shared_future&& rhs) noexcept; shared_future&运算符=(shared_future && rhs)noexcept; 12 Effects: 12种效果:

— releases any shared state (30.6.4); —释放任何共享状态(30.6.4);

— move assigns the contents of rhs to *this — move将rhs的内容分配给* this

Question Part 1 depends solely on releasing a shared state , eg 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. 问题第1部分仅取决于释放共享状态 ,例如在阅读§30.6.4之后, 销毁共享状态 ,因此我想这意味着第1部分应该为true ,但我不确定。

Question Part 2 seems to be false , because these are two steps and I neither 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() . 问题第二部分似乎是错误的 ,因为这是两个步骤,我既不知道移动部分是否是原子的,也不知道如果其他线程位于shared_future::get()共享状态被破坏,会发生什么情况。

These are only notes in [futures.shared_future], but they're relevant: 这些只是[futures.shared_future]中的注释,但它们是相关的:

[ Note: Member functions of shared_future do not synchronize with themselves , but they synchronize with the shared shared state. [注意: shared_future的成员函数不与自己同步 ,但与共享的共享状态同步。 —end note ] —尾注]

[...] [...]

 const R& shared_future::get() const; R& shared_future<R&>::get() const; void shared_future<void>::get() const; 

Note: access to a value object stored in the shared state is unsynchronized , so programmers should apply only those operations on R that do not introduce a data race (1.10). 注意:对共享状态下存储的值对象的访问是不同步的 ,因此程序员仅应在R上应用那些不会引入数据竞争的操作(1.10)。

So calling change() is fine as long as nobody is calling read() or otherwise accessing sf . 因此,只要没有人调用read()或以其他方式访问sf则调用change()很好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我们什么时候需要 std::shared_future 而不是 std::future 来进行线程间同步? - When do we need std::shared_future instead of std::future for inter-thread synchronization? Raspberry Pi工具链上的std :: shared_future - std::shared_future on Raspberry Pi toolchain std :: future或std :: shared_future等待多个线程 - std::future or std::shared_future to wait for multiple threads std :: async和std :: shared_future导致程序崩溃 - std::async and std::shared_future causes the program to fall C++ 什么是 std::shared_future 和 std::promise - C++ what are std::shared_future and std::promise 将 std::shared_future 作为函数的引用传递是否合法? - Is it legal to pass `std::shared_future` as a reference to functions? 为什么std :: future <T> 和std :: shared_future <T> 不提供成员swap()? - Why do std::future<T> and std::shared_future<T> not provide member swap()? future和shared_future有什么区别? - What is the difference between future and shared_future? 响应需要其引用共享状态的std :: shared_future的OWN COPY的线程 - Reacting threads needing its OWN COPY of the std::shared_future that refers to the share state 在同一个线程中的同一个实例上多次调用shared_future :: get()是否合法? - Is it legal to call shared_future::get() multiple times on the same instance in the same thread?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM