Accessing atomic of C++0x as non-atomic

前端 未结 3 1737
被撕碎了的回忆
被撕碎了的回忆 2020-12-19 14:50

I have an atomic variable in my program of type atomic. At some places I don\'t need to access the value in it atomically, as I just check if its 0 o

3条回答
  •  不知归路
    2020-12-19 15:02

    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 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!

提交回复
热议问题