简体   繁体   中英

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

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?

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.

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.

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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