[LLVMdev] Is it correct to access non-atomic variables using atomic operations?
Eli Friedman
eli.friedman at gmail.com
Fri Aug 3 15:16:08 PDT 2012
On Fri, Aug 3, 2012 at 3:11 PM, Lei Zhao <leizhao833 at gmail.com> wrote:
> Hi Everyone,
>
> This might be more a C/C++11 question than a LLVM question: I wondering if it is semantically correct to access an ordinary variable via atomic operations, like:
>
> int val;
> void foo(){ atomic_store((atomic_int *)(&val), 42); }
>
> I tried this simple program on clang+llvm and 42 seems to be correctly stored to val. But, is this just by luck or a right way to go?
Strictly speaking, it's undefined behavior; in practice, it'll do what
you expect (with the disclaimer that it's generally a bad idea to mix
atomic and non-atomic accesses to a variable).
-Eli
More information about the llvm-dev
mailing list