50% cpu?

Hi, can someone explain this? Why 50% CPU? Thank you soooo much…

Code:

#include <stdio.h>
#include <SDL.h>

int main(int argc, char* argv[]) {
bool done = 0;

if (SDL_Init(SDL_INIT_VIDEO) < 0) {
	printf("Error at startup.");
	exit(1);
}

while (!done) {
	//code
}
return 0;

}

[Image: http://img389.imageshack.us/img389/6897/dibujons.jpg ]

Infinite loops take all the CPU there is, that is normal. I am guessing you have dual-core processor and one of the cores is used in 100% hence 50% reported.

If you want to lower CPU usage put SDL_Delay(10) call at the end of loop iteration and see what happens.

Yep. On Windows you can sleep for 0 milliseconds and that’s enough (this tells Windows that your application wants to wait, which means it can go do other stuff for other processes).
Dunno about Linux or Mac though.------------------------
EM3 Nathaniel Fries, U.S. Navy

Mac OS X provides pretty much the same APIs as Linux (pthreads) when it comes
to these things, but I’m not sure what SDL does.

Either way, SDL_Delay(0) doesn’t work on Linux, at least not with any SDL
version I’ve tried. In my SDL binding for EEL (and probably in some other
projects of mine as well), I do this:

static EEL_xno eb_Delay(EEL_vm *vm)
{
Uint32 d = eel_v2l(vm->heap + vm->argv);
#ifdef _WIN32
SDL_Delay(d);
#else
if(!d)
sched_yield();
else
SDL_Delay(d);
#endif
return 0;
}On Saturday 03 April 2010, at 23.03.55, “Nathaniel J Fries” wrote:

Yep. On Windows you can sleep for 0 milliseconds and that’s enough (this
tells Windows that your application wants to wait, which means it can go
do other stuff for other processes). Dunno about Linux or Mac though.


//David Olofson - Developer, Artist, Open Source Advocate

.— Games, examples, libraries, scripting, sound, music, graphics —.
| http://olofson.net http://kobodeluxe.com http://audiality.org |
| http://eel.olofson.net http://zeespace.net http://reologica.se |
’---------------------------------------------------------------------’