Thread: potential problems?

  1. #1
    Registered User
    Join Date
    Aug 2010
    Posts
    15

    potential problems?

    Hey everyone, just getting back into the groove again. I built this function and was wondering if there was anything wrong with it that might manifest later. It works fine that I can see, but it is eventually for use in game programming and such.

    Edit: Actually, this would probably be unusable for what I had in mind. Would need something that times in milliseconds. Still, how does the code look?

    Code:
    void sleep(int seconds) {
    	int time = 0;
    	clock_t pre_time, post_time;
    
    	pre_time = clock();
    	while (time < seconds) {
    		post_time = clock();
    		if ((post_time/CLOCKS_PER_SEC) >= ((pre_time/CLOCKS_PER_SEC)+1)) {
    			time++;
    			pre_time = post_time;
    		}
    	}
    	return;
    }
    Last edited by deepcode; 08-11-2010 at 12:59 PM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    Well it's going to burn a lot of CPU time doing nothing.

    Is your game likely to want to be doing anything else?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    That's just a busy loop that periodically checks how much CPU time has been burned. That's generally not what you want.

    Your OS probably provides a way to sleep for a certain amount of time; and if you really need to be portable, this is a place where I'd recommend using platform #ifdefs.

    Even if you do go this route, clock() is not measuring wall time, but CPU time. If other processes are using the CPU, your sleep could take a lot longer than you expect. I tried to sleep(2) while intentionally burning the CPUs with other processes; it took nearly 4 seconds. My platform's native sleep() took 2 seconds regardless of what else was going on.

  4. #4
    Registered User
    Join Date
    Aug 2010
    Posts
    15
    That's what worried me. Was unsure on clock, it just looked easier :P

    Would I be able to achieve the same effect using the tm struct and using time and difftime from the standard lib? Or do they play off the same thing, clock cycles?

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    Sire, just try them both with task manager running in the background, and watch the CPU hit 100%

    Then try it with something like sleep()
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Common Problems
    By WolfPack in forum C Programming
    Replies: 4
    Last Post: 01-28-2003, 06:38 PM
  2. Integration problems
    By Shiro in forum A Brief History of Cprogramming.com
    Replies: 33
    Last Post: 01-14-2003, 02:32 PM
  3. tile colliision detection problems
    By werdy666 in forum Game Programming
    Replies: 1
    Last Post: 10-23-2002, 09:26 PM
  4. Coding Problems
    By RpiMatty in forum C++ Programming
    Replies: 12
    Last Post: 01-06-2002, 02:47 AM
  5. Problems with my RPG app
    By valar_king in forum Game Programming
    Replies: 1
    Last Post: 12-15-2001, 08:07 PM