如何访问 std::atomic的值作为非原子的
我的程序中有一个 atomic
类型的原子变量。在某些地方,我不需要原子地访问其中的值,因为我只需检查它是否为 0。换句话说,在这些情况下,我想避免原子访问时发生的总线锁定等开销。
如何以非原子方式访问原子变量。使用 (int) 对其进行类型转换是否足够,如下所示?如果没有,我想我该怎么做?
atomic<int> atm;
int x;
........
x = (int)atm; // Would this be a non-atomic access, no bus locking et all?
I have an atomic variable in my program of type atomic<int>
. At some places I don't need to access the value in it atomically, as I just check if its 0 or not. In other words, at those instances I want to avoid the overhead of bus locking etc. that happens when there is atomic access.
How can I access the atomic variable non-atomically. Is typecasting it with (int) enough, like as follows? If not, which I think, how can I do this?
atomic<int> atm;
int x;
........
x = (int)atm; // Would this be a non-atomic access, no bus locking et all?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你无法摆脱原子性属性。但是,您也许可以通过放宽内存排序保证来减少使用原子变量所涉及的一些开销。
不过,我不建议这样做,并且我同意所有敦促您避免这样做的评论。您确定您为原子操作付出了足够高的成本,以至于进行这种黑客攻击并可能引入线程错误是值得的吗?
You can't get rid of the atomicity property. But you might be able to reduce some of the overhead involved in the use of atomic variables by relaxing the memory ordering guarantees.
I wouldn't recommend doing this however, and I echo all the comments urging you to avoid this. Are you sure that you're paying a high enough cost for the atomic operations that doing this hack and potentially introducing threading bugs is worth it?
在大多数平台上,读取 int (尤其是对齐的 int,即堆栈变量)无论如何都是原子的,因此将其分配给 int 无论如何都是一个简单的分配。
如果如上所述无法以原子方式访问该变量,那么您仍然需要将其分配以保证其不被写一半。
即只需使用atomic<>即可。多变的。它很好而且更安全。
On most platforms reading an int (especially an aligned one, which a stack variable will be) will be atomic anyway so just assigning it to an int will be a simple assignment anyway.
If the variable isn't accessible atomically as outlined above then you still need to just assign it across to guarantee its not half-written ..
ie Just use the atomic<> variable. Its fine and much safer.
我怀疑这是否有效,因为提供给强制转换的值仍然必须从原子中获取。我浏览了 std::atomic<整数>专业化声明 但据我所知,从他们对基类 std::atomic 的声明来看,没有办法访问底层变量。
那里有宏声明应该能够告诉您是否在您的平台上使用了锁,尽管我不确定这些是标准方法还是扩展方法。最坏的情况是,您可以只评估宏
ATOMIC_INT_LOCK_FREE
并查看它是否存在。 (注意:atomic
还可以处理其他事情,例如确保内存位于正确的边界上等),尽管这对您的代码来说并不重要;它要么会,要么不会,而且似乎没有一个定义的方法来获取 int。您也可以只是玩弄它,通过将其设置为已知值来查看原子,然后使用调试器检查它或获取其地址并将其打印出来。您可以像这样玩一会儿,并可能找到一种方法来执行某种(不可移植的、非标准的)指针操作来获取指向该值的指针,然后将其存储在您想要执行非操作的任何地方。 - 原子检查。
希望您找到您需要的东西!
I doubt that will work since the value fed to the cast will still have to be obtained from the atomic. I looked through the std::atomic< int > specialization declaration but as far as I can tell from that, and from their declaration of the base class std::atomic, there is no way to access the underlying variable.
There are macro declarations there that should be able to tell you whether a lock is used at all for your platform, although I'm not sure if these are standard or extension methods. Worst case, you could just evaluate the macro
ATOMIC_INT_LOCK_FREE
and see if it exists. (note:atomic<int>
handles other things as well, such as ensuring the memory is on proper boundaries, etc.), although it won't matter much for your code; it either will or will not be and there doesn't seem to be a defined way to get at the int.It's also possible you could just play around with it and look at an atomic by setting it to a known value and then inspecting it with a debugger or by taking its address and printing it out. You could play around like that for a bit and probably figure out a way to do some sort of (non-portable, non-standard) pointer manipulation to get a pointer to the value, then store that outside wherever you want to do your non-atomic check.
Hope you find what you need!