3 ms·
What if the thread is preempted between the two lines? Another thread should be able to read and modify that variable, no?
by bzb5 6y ago
What if the thread is preempted between the two lines? Another thread should be able to read and modify that variable, no?
- asdfasgasdgasdg 6y agoFor C standards before C11, C doesn't have a thread-aware memory model. The notion of threads suspending and waking up, or memory access from one thread's stack to another, does not exist in the standard, at least as far as I'm aware. The C standard also doesn't define the memory layout of the stack, so there is no standard way to acquire the address of that variable. It's easy enough to see that compilers do not actually create storage for it: https://godbolt.org/z/cfE9o5 https://godbolt.org/z/cfE9o5 In C11 there is a thread aware memory model, but still no standard way to access that location, so compilers will assume it doesn't need to be created. https://godbolt.org/z/KTz9sT https://godbolt.org/z/KTz9sT
- mhh__ 6y agoIn what language is it defined behaviour to grab a thread and start playing with its memory? The compiler can only know what the standard says and in this case it would be fair game