lundi 27 mai 2019

Volatile cast inside of while loop

I noticed that unless I use something like a Sleep(1) inside of my while loop, it will never break.

while (*Ns::Gl::G == nullptr) Sleep(1); // Works

while (*Ns::Gl::G == nullptr); // Doesn't work

After some research I would think it's because *Ns::Gl::G is changed by another thread or process, this seems likely in my scenario.

So, I tried to do something amongst the lines of:

while (*(volatile TYPE **)&*Ns::Gl::G == nullptr); amongst a lot of other variants of that, and all to no avail.

The way Ns::Gl::G is defined is amongst the lines of:

namespace Ns { struct Gl final { static TYPE** G }; TYPE** Gl::G;}

Does anyone have an idea of how I can turn *Ns::Gl::G into volatile inside of the while loop? MemoryBarrier() works but I'd highly prefer to use the volatile keyword.

TL;DR - volatile cast inside of while loop isn't working due to compiler optimizations~

Aucun commentaire:

Enregistrer un commentaire