On Mon, 6 Jan 2025 at 16:13, Jeff Law <jeffreya...@gmail.com> wrote:
>
> But in the case of concurrent accesses, shouldn't these objects be
> declared as atomic?

No.

They aren't concurrent accesses to the same variable.

They are concurrent accesses to *different* memory locations, and the
compiler is not allowed to mess them up.

IOW, if you have

    struct myvar {
        pthread_mutex_t buffer_lock;
        pthread_mutex_t another_var_lock;
        char buffer[7];
        char another_var;
    };

and "buffer_lock" serializes accesses to "buffer[]", and
"another_var_lock" serializes accesses to "another_var", then the
compiler IS NOT ALLOWED TO TOUCH "another_var" when the code touches
"buffer[]".

So if a compiler turns "memset(var->buffer, 0, 7)" into "load 8 bytes,
clear 7 of the bytes, store 8 bytes", then the compiler is buggy.

Because that messes up another thread that accesses "another_var", and
the 8-byte write may write back an old value that is no longer valid.

There is absolutely no gray area here. It was always buggy, and the
alpha architecture was always completely and fundamentally
mis-designed.

C11 made it explicitly clear:

  "Different threads of execution are always allowed to access (read
and modify) different memory locations concurrently, with no
interference and no synchronization requirements"

but honestly, that was just codifying something that should have been
blindingly obvious even before.

                Linus

Reply via email to