Edited the Subject.
What are you actually trying to achieve? The sample code does not actually
DO anything useful, even if it's technically possible to find ways to
observe the value of `i` and `j` outside of `main`.
What do you expect the compiler to do with this?
If nothing else is using `i` and `j`, what's the purpose of changing their
If you ARE using those values elsewhere, the using `volatile` is indeed the
right thing to do. Or calling some functions inside `main`, that will cause
the compiler to understand that "this does something more than just update
+1 for using either volatile or atomics, depending on precisely what your goal is.
Anyone from the community ,please share their insights why above
transformation is right ?
This looks like it might be helpful to you (there’s a C-specific answer at the bottom).
Thank you for coming back with your views and consider the application
is multi-threaded .
Thank you again.