Home > database >  Pausing a for loop until a condition is met in c
Pausing a for loop until a condition is met in c

Time:10-03

I am making a program, in which I have a for loop. I want the for loop to wait until a bool turns true and then continue.

It is a dll for a game (hack) so the bool isInWorld automatically changes depending on if player is in a world or not.

bool isInWorld; //it is an in game variable which I have here, so if i am in a world, it is true. otherwise its false.
for (int x = 0; x < worldSize.x; x  )
{
    for (int y = 0; y < worldSize.y; y  )
    {
    //Wait until isInWorld turns true
    //do something
    }
}

CodePudding user response:

 for (int x = 0; x < worldSize.x; x  )
 {
      for (int y = 0; y < worldSize.y; y  ) 
      {
           while(isInWorld) { // while loop will only works when isInworld return true
           //do something
           }
      }
   
 }

and if you want inside for loop to execute when isInWorld return true, then put whole inner for loop in while loop like given below.

bool isInWorld; 

for (int x = 0; x < worldSize.x; x  )
{
     while(isInWorld) {
           for (int y = 0; y < worldSize.y; y  ) // this for loop will only execute if isInWorld returns true
           {
                //do something
           }
    }
}

CodePudding user response:

I'm afraid that there is no reliable solution to your problem.

If the variable isInWorld can be changed by another thread at any time, then, in order to solve this problem reliably, you require proper thread synchronization between your thread and the game's threads. Since you say that this is a "hack", then you probably don't have access to the game's source code, so you cannot introduce thread synchronization into the game's threads.

If you don't use thread synchronization, then you will be unable to avoid a TOCTOU race condition. Even after verifying that the variable isInWorld has the value true, you cannot assume that the variable will stay true while you are doing your work. If it changes to false, then you won't notice until you check again.

The safest way to read data out of the game would probably be to suspend all of the game's threads, before reading the data. That way, the data of the game won't be modified by one of these threads, while you are reading the data. However, there is still the danger that the data was in an inconsistent state at the time that the threads were suspended, so the data you read could be corrupt.

In your question, you state that you are using a DLL. Therefore, I assume that you are using Microsoft Windows. On Microsoft Windows, you can use the function SuspendThread to suspend a thread.

Irrespective of whether the other threads are suspended or not, you could try using the following code to read the data:

//wait until variable has the desired value
while ( !isInWorld )
{
    //do nothing
}

for ( int x = 0; x < worldSize.x; x   )
{
    for ( int y = 0; y < worldSize.y; y   )
    {
        //do something
    }
}

Note that this code does busy-waiting, which causes the thread's CPU usage to be at 100%. This will likely hurt the performance of the game and have other negative effects, such as increased power usage. Therefore, it is normally better to use a wait function to allow the thread to sleep while waiting for the condition to be true. However, in this case, this is not possible, because this would require the cooperation of the other game's threads. Therefore, in order to prevent 100% CPU usage, you probably can't do much more than call Sleep or std::this_thread::sleep_for to make the thread sleep for a certain time period, before checking again.

CodePudding user response:

bool a_bool = false;  // maybe volatile
for (; !a_bool;) { /* ... */ ; }
  •  Tags:  
  • c
  • Related