std :: atomic是否有可能使类的复杂成员函数成为atomic? [英] Is it possible with std::atomic, make a complex member function of a class atomic?

查看:277
本文介绍了std :: atomic是否有可能使类的复杂成员函数成为atomic?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有可能做这样的事情吗?

Is it possible to make something like this?

...
class test{
int i;
public:
      test(int k):i(k){};
      void my(){
         cout<<i;
      }
}
atomic<test> kk(0);
kk.test();
...

如果这不可能,那么如何调用函数,使其成为原子的?

If this is not possible then how to make a call of function so that it will was atomic?

推荐答案

std::atomic的工作方式是使用处理器提供的某些指令.这些指令仅适用于某些大小的整数(不同的处理器对可以做什么和不可以做什么有不同的限制和规则,在某些体系结构中,处理器体系结构甚至可能需要使用互斥锁或类似功能来简单地实现std :: atomic).

The way that std::atomic works is that it uses certain instructions provided by the processor. These instructions are ONLY available for integers of certain sizes (different processors have different limits and rules about what you can and can't do, and in some architectures, the processor architecture may even require the use of a mutex or similar functionality simply to implement std::atomic).

还要注意,std::atomic的目的是用来确保在多个处理器核心或多个处理器之间原子地更新该值,这通常不是您想要/不能对较大的数据结构执行的操作.

Note also that the purpose of std::atomic is used to ensure that the value is updated atomically across multiple processor cores or multiple processors, which is not typically what you want/can to do with larger data structures.

要在其他数据结构上实现原子操作,您将必须使用互斥锁或类似的构造,以确保以线程原子"方式(不同于处理器原子")完成处理.

To achieve atomic operations on other data structures, you will have to use mutex or similar constructs to ensure that the processing is done in an "thread atomic" way (different from "processor atomic").

这篇关于std :: atomic是否有可能使类的复杂成员函数成为atomic?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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