简体   繁体   English

访问C ++ 0x的原子<int>为非原子

[英]Accessing atomic<int> of C++0x as non-atomic

I have an atomic variable in my program of type atomic<int> . 我的类型为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. 在某些地方,我不需要原子地访问它中的值,因为我只是检查它是否为0。 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? 是否使用(int)进行类型转换,如下所示? 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?

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. 但是,您可以通过放宽内存排序保证来减少使用原子变量所涉及的一些开销。

std::atomic<int> a;

int value = a.load(std::memory_order_relaxed);
if(value == 0) {
    // blah!
}

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? 你确定你为原子操作支付了足够高的成本吗?做这个黑客并可能引入线程错误值得吗?

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. 在大多数平台上读取一个int(特别是一个对齐的,一个堆栈变量将是)无论如何都是原子的,所以只需将它分配给一个int将是一个简单的赋值。

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. 它很好,更安全。

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. 我查看了std :: atomic <int>特化声明,但据我所知,从它们的基类std :: atomic的声明中,没有办法访问底层变量。

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. 最糟糕的情况是,您可以只评估宏ATOMIC_INT_LOCK_FREE并查看它是否存在。 (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; (注意: atomic<int>处理其他事情,例如确保内存位于适当的边界等),尽管对你的代码来说并不重要; it either will or will not be and there doesn't seem to be a defined way to get at the int. 它要么会或者不会,并且似乎没有一种定义的方式来获得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! 希望你找到你需要的东西!

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM