Do atomic_store / load from <stdatomic.h> work for non-standard, cross-cache data on Intel?

Are the data stored in atom_store and loaded with atomic_load always saved?

In particular: the C11 program refers to 64-bit data placed intentionally on the border between cache lines on a modern Intel processor. It uses atomic_store and atomic_load (from <stdatomic.h>) to access this data from multiple threads (running on different cores).

Will the data always be displayed consistent or load it (atomic_load), sometimes have some bytes belonging to the old value, and other bytes belonging to the newer value?

Here are the basic definitions of structures and variables and the interesting part of the program that takes place in a loop, in parallel from several threads:

struct Data {
    uint8_t bytes[CACHELINE__BYTECOUNT - 4];
    atomic_uint_fast64_t u64;
} __attribute__((packed)) __attribute__((aligned ((CACHELINE__BYTECOUNT))));

#define VAL1 (0x1111111111111111)
#define VAL2 (0xFFFFFFFFFFFFFFFF)

static struct Data data = { .u64 = VAL1 };

...

    for (uint32_t j = 0; j < 1000; j++) {
        atomic_store(&data.u64, VAL1);
        atomic_store(&data.u64, VAL2);
    }
    const uint64_t val = atomic_load(&data.u64);
    /* is 'val' always VAL1 or VAL2? */

( : https://gist.github.com/sinelaw/1230d4675d6a4fff394110f17e463954)

gcc 6.3.0 clang 3.7 , :

$ clang -std=c11 -Wall -Wextra /tmp/atomic.c -o /tmp/atomic -lpthread
$ /tmp/atomic
ERROR: oh no, got: 11111111FFFFFFFF

, <stdatomic.h>, .

+6
1

, . int64 .

, : . , (__attribute__), .

, , , stdatomic , , , stdatomic.

+8

All Articles