Executing two while-loops at same time

Hello

I need to execute two while loops at exactly the same time.

One while loop (A) is a timeout,
the other one (B) is the actual code which need to check things.

So, A works like this:
I've putted an integer with the value of 20 into a while loop, every time the integer gets dropped by one (20, 19, 18, 17,...), if that integer finally hits zero; it returns a value saying "false", so B terminates. Because B only runs when the value is true.

Pseudo code:
1
2
3
4
5
6
7
8
9
10
11
12
bool bool1 = true;
int int1 = 20;
while (bool1) {
	while (bool1) {
		int1--;
		sf::sleep(1); //SFML library for timeout of one second,
		if (int1 == 0) {
			bool1 = false;
		}
	}
/*----------ACTUAL CODE BENEATH THIS LINE----------*/
}


The while-loop indeed stops; but the 'actual code' isn't terminated.
How can I do this?

Thanks for reading,
Niely
Last edited on
As you are already using SFML perhaps you can look at sf::Thread. This allows you to launch "parallel" processes which sounds like what you wish to accomplish.

http://www.sfml-dev.org/documentation/2.2/classsf_1_1Thread.php
Does other loop run in other thread?
try
volatile bool bool1 = true;
I don't think threads are what I'm looking for.
I'd have to work with different functions, and because you can't go from void to main, and variables ain't global,... I'd encounter to much problems.
Last edited on
I don't think threads are what I'm looking for.
I'd have to work with different functions, and because you can't go from void to main, and variables ain't global,... I'd encounter to much problems.
What are you actually trying to do?

You have tried to simplify your scenario for us, but you have simplified it incorrectly.
Last edited on
The user has a few seconds to quit my application.
For that I use the SFML keyboard-events, the user only has a few seconds to press 'quit'.

Threads would work in theory, but it'd get messy with my variables etc. If I could declare variables globally in each function it'd be okay to work with threads. And jumping from function to main again also.
Last edited on
The problem with your example is that the "CODE BENEATH THIS LINE" won't ever be evaluated until the first while loop goes all the way through the timeout. The code in the loops cannot run concurrently unless you employ threads, which you stated would add a higher degree of complexity to your code.
I don't know why you would want that, but the general idea is that you want a button to disappear after a certain amount of time.

That's simple enough. At the beginning of your application, store the time:

auto start = std::chrono::system_clock::now();

Then while drawing your application, check if the amount of time you want has elapsed before drawing the button:
1
2
3
4
if(std::chrono::system_clock::now() - start < std::chrono::seconds{5})
{
    window.draw(button);
}
Don't forget to perform a similar check for your click-handling code.

See also: http://en.cppreference.com/w/cpp/header/chrono
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
bool bool1 = true;
int int1 = 20;
while (bool1) {
	while (bool1) {
		int1--;
		sf::sleep(1); //SFML library for timeout of one second,
		if (int1 == 0) {
			bool1 = false;
		}
	}
        if (!bool1) {
            /*----------ACTUAL CODE BENEATH THIS LINE----------*/
        }
}


Alternative:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
for (int timeout = 20; timeout > 0; timeout--) {
    sf::sleep(1); //SFML library for timeout of one second,

    ///////////////////////////////////////////////////////////////////////////
    //        logic to get user input from keyboard and quit if necessary.   //
    //             set timeout to 0 if user decides to quit                  //
    ///////////////////////////////////////////////////////////////////////////

  

    if (timeout == 1) {
         /*----------ACTUAL CODE BENEATH THIS LINE----------*/
        
        timeout = 21;
    }
}
Last edited on
Thanks all! :)

Got it to work, used SFML threads and changed my variables a bit etc.
Using threads for this is overkill. You're using a titan to squish a spider in your house. It is very likely to smash your house to pieces.
Last edited on
Topic archived. No new replies allowed.