Page 1 of 1

Wait Function

Posted: Sat Sep 10, 2011 5:29 pm
by Light-Dark
i've done my googling searching and it seams im getting the same results the Sleep() function, but thats not what im looking for im looking for a function that will let my main loop continue running but in a function it will hold it up from executing the next line or command for a specified period of time, im just wondering if such a thing is even possible and if so how would i go about doing it :oops:

Re: Wait Function

Posted: Sat Sep 10, 2011 6:04 pm
by avansc
you would have to run that function in a seperate thread. just look into threading.

Re: Wait Function

Posted: Sat Sep 10, 2011 6:20 pm
by Light-Dark
avansc wrote:you would have to run that function in a seperate thread. just look into threading.
oh thanks

Re: Wait Function

Posted: Sat Sep 10, 2011 10:43 pm
by k1net1k
another happy customer

Re: Wait Function

Posted: Mon Sep 12, 2011 6:35 pm
by superLED
This is how I go about it. Works pretty well.

Code: Select all

#include <iostream>
#include <time.h>
using namespace std;

bool myFunction(double mDiff);

int main() {
	time_t lastTime, currentTime;	// Creating our time trackers

	time(&lastTime);				// Setting "lastTime" to the current time (the time the program stars)
	
	double diff;					// To check how much time has passed

	while(true) {
		time(&currentTime);			// At the start of every loop, update currentTime to the current time
		diff = difftime(currentTime, lastTime);

		if(myFunction(diff)) {		// Run the function, and if it returns true, 
			lastTime = currentTime;	// THen update the lastTime to the currentTime
		}
	}

	return 0;
}

bool myFunction(double mDiff) {
	if(mDiff >= 1) {				// If 1 second has passed
		cout << "This will be printed every 1 sec." << endl;	// Then write this line
		return true;											// And return true
	} else {
		return false;				// If not, return false
	}
}
No need for extra threads or freezing the program.

Re: Wait Function

Posted: Tue Sep 13, 2011 9:35 am
by Falco Girgis
superLED wrote:This is how I go about it. Works pretty well.

Code: Select all

#include <iostream>
#include <time.h>
using namespace std;

bool myFunction(double mDiff);

int main() {
	time_t lastTime, currentTime;	// Creating our time trackers

	time(&lastTime);				// Setting "lastTime" to the current time (the time the program stars)
	
	double diff;					// To check how much time has passed

	while(true) {
		time(&currentTime);			// At the start of every loop, update currentTime to the current time
		diff = difftime(currentTime, lastTime);

		if(myFunction(diff)) {		// Run the function, and if it returns true, 
			lastTime = currentTime;	// THen update the lastTime to the currentTime
		}
	}

	return 0;
}

bool myFunction(double mDiff) {
	if(mDiff >= 1) {				// If 1 second has passed
		cout << "This will be printed every 1 sec." << endl;	// Then write this line
		return true;											// And return true
	} else {
		return false;				// If not, return false
	}
}
No need for extra threads or freezing the program.
Out of curiosity, what kind of logic do you have in that function that makes you want to force it to run no more frequently than once a second?

Re: Wait Function

Posted: Tue Sep 13, 2011 7:13 pm
by superLED
Since he doesn't want the rest of the program to freeze, I believed it was a matter of at least one second. If it's a matter of few milliseconds, the wait wouldn't matter much, unless things have to run really fast (a game or similar).
I guess he could use GetTickCount() instead. That function returns the time in milliseconds since the program started. And do calculations from that. I have never used that function, so I'm not totally sure.

When doing game programming, and I may need some millisecond persistence, I use SDL_GetTicks()