1

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:

  1. I can't see how it could affect the loop since it is read only once while initializing previousDrawn;
  2. 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;
  3. 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.

me myself
  • 23
  • 4
  • 4
    Breaks [intro.progress](https://eel.is/c++draft/intro.progress). – user4581301 Jan 03 '23 at 19:26
  • 1
    What do you get when you unwind an empty loop? The rule that @user4581301 cited permits the compiler to unwind an empty loop **even if the number of iterations could be infinite** because the case with infinite iterations is not a well-behaved C++ program. – Ben Voigt Jan 03 '23 at 19:28
  • 2
    Once you get beyond asking what's wrong with your failed attempt, you'll want a solution. That would be "synchronization primitives" like mutexes, condition variables, or barriers. From your description "Since it is essential for some instruction to be executed in a certain order ... wait for one to be finished in order for the next one to start." you're looking for a barrier. – Ben Voigt Jan 03 '23 at 19:31
  • 3
    Some people, when confronted with a problem, think, “I know, I’ll use threads,” and then two they hav erpoblesms. – Slava Jan 03 '23 at 19:36
  • 2
    If some instructions have to be executed in a particular order they should be in the same thread. Trying to enforce a particular order of execution across threads defeats the purpose of having threads. – Pete Becker Jan 03 '23 at 19:42

0 Answers0