我必须显式地调用原子加载/存储吗? [英] Must I call atomic load/store explicitly?

查看:76
本文介绍了我必须显式地调用原子加载/存储吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C ++ 11介绍了 std :: atomic<> 模板库。该标准指定 store() load()操作以原子设置/获取由多个共享的变量线程。

C++11 introduced the std::atomic<> template library. The standard specifies the store() and load() operations to atomically set / get a variable shared by more than one thread.

我的问题是分配和访问操作也是原子的?

My question is are assignment and access operations also atomic?

即:

std::atomic<bool> stop(false);
...
void thread_1_run_until_stopped()
{
    if(!stop.load())
        /* do stuff */
}

void thread_2_set_stop()
{        
    stop.store(true);
}

等效于:

void thread_1_run_until_stopped()
{
    if(!stop)
        /* do stuff */
}

void thread_2_set_stop()
{        
    stop = true;
}


推荐答案


非引用类型的赋值操作和访问操作是否也是原子操作?

Are assignment and access operations for non-reference types also atomic?

是的, atomic< T> :: operator T atomic< T> :: operator = 等效于 atomic :: load atomic :: store 。所有的运算符都是在原子类中实现的,所以它们会像你所期望的那样使用原子操作。

Yes, they are. atomic<T>::operator T and atomic<T>::operator= are equivalent to atomic<T>::load and atomic<T>::store respectively. All the operators are implemented in the atomic class such that they will use atomic operations as you would expect.

我不知道你对非引用类型?不确定参考类型在这里是如何相关。

I'm not sure what you mean about "non-reference" types? Not sure how reference types are relevant here.

这篇关于我必须显式地调用原子加载/存储吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆