How can a structured atomic type be blocked?

I found the following code: there is always:

std::atomic<A> is lock free? false
std::atomic<B> is lock free? true

This is the code:

struct A { int a[100]; };
struct B { int x, y; };
int main()
{
    std::cout << std::boolalpha
              << "std::atomic<A> is lock free? "
              << std::atomic<A>{}.is_lock_free() << '\n'
              << "std::atomic<B> is lock free? "
              << std::atomic<B>{}.is_lock_free() << '\n';
}

I don’t understand how the second specialized atomic type of the structure is blocked and the 1st specialized atomic type cannot be blocked?

Thanks in advance.

+4
source share
2 answers

http://en.cppreference.com/w/cpp/atomic/atomic_is_lock_free really explains this in the comments section. The alignment and size of the registers can allow the processing of 2 packed ints in an atomic way. In other words, the 2 matched ints are no different from the one long long in a 64-bit system with a 128-bit register.

+5

std::atomic , . , (, ..) B , .

+2

Source: https://habr.com/ru/post/1684921/


All Articles