I've been working on a personal project for a while and recently tried to implement threads to improve performance. Since it is essential for some instruction to be executed in a certain order I've tried implementing an extremely rudimentary way to wait for one to be finished in order for the next one to start.
While doing so I've encountered the "problem" described in the title in the following lines of code.
bool previousDrawn = drawnTriangles[eye][i - 1];
while (previousDrawn == false) {}
Somehow, even when previousDrawn
is initialized by drawnTriangles[eye][i - 1]
as false
the program doesn't get stuck in a loop.
These lines are part of a function executed multiple times simultaneously by threads, however to my knowledge a thread's variable such as previousDrawn
shouldn't be affected by wathever is going on inside the other threads.
This is the only conclusion I could think of since there aren't any instructions neither inside the loop (since it's empty) nor between the loop and previousDrawn
's initialization that could modify it's value.
Admittedly drawnTriangles
is a vector shared between the threads but I didn't consider it a potential cause since:
- I can't see how it could affect the loop since it is read only once while initializing
previousDrawn
; - Even then, it would be not only extremely unlikely but impossible for 2 threads to read it at the same time based on how the function works;
- As I've said before and as it's visible in the code, the components of the shared vector are only read by the threads and to my knowledge threads reading a variable simultaneously doesn't cause problems.
Any help would be greatly appreciated, so please let me know if and what I am getting wrong and/or am missing about this because I'm truly at a loss.
Edit: I forgot to mention that I had also assumed that it probably wasn't the compiler since I tried modifying the loop's condition as
while(false == false) {}
And the program did freeze.