Skip to main content
GameDev.net gamedev.net
🔒 Locked

SDL and thread problem

Started by LPVOID_CH Oct 30, 2006 at 1:57 PM 2 replies 800+ views
Original Post
LPVOID_CH
LPVOID_CH
hi all i'm using SDL for my game and i use several threads (rendering and networking). in each thread i have a delay for 20 to 60ms. when i run the game, the processor load is 100%. if i disable the threads, the processor load is normal. even the cpu load is 100%, the game runs without problems. do i have to add somthing to my SDL threads, so that the cpu load is normal?
------------------------<< deltasoft games >>Homepage: http://www.deltasoftgames.ch
Nychold
Nychold
I must say that I don't know anything about SDL, but with multithreaded programming, you'll often get 100% CPU usage, even when you're not at 100% usage, especially if you or part of your code is using mutex locks. See, what happens is, one of your threads is trying to do something with memory, while another thread tries to do something else. One of the threads has to wait, and changes are, to keep the wait loop simple, it's probably:

bool isLocked=false;void some_mutex_lock_function(){  while (isLocked);  isLocked=true;}void some_mutex_unlock_function(){  isLocked=false;}


While in the some_mutex_lock_function, you're using 100% of CPU power, even though you're not doing anything. If you can, try to eliminate all locks, and where you have a lock, try to return control to the OS if possible. Like I said, I don't know SDL, so all of this code might be in SDL itself.
LPVOID_CH
LPVOID_CH
thanks for your approach, but i am already familiar with mutex and locks. i've also implemented them. but i did a simple app, which uses one thread without locking, there is only a while loop like this.:

void some_thread() {

while (!done) {
SDL_Delay(30);
}
}

if i disable this thread and run only the main thread the cpu usage is 0%. then by enabling this thread it is 100%.
------------------------<< deltasoft games >>Homepage: http://www.deltasoftgames.ch
Kylotan
Kylotan
Nychold, that's not true... you can't have "100% usage, even when you're not at 100% usage" because a while loop like that is 100% usage. But that is not how mutexes are implemented on any sensible platform anyway, so it's not relevant.

LPVOID_CH, it may just be the case that your threads are interleaving to take full advantage of the CPU time. Or maybe the measurement is wrong. It's hard to say without seeing what is in both threads.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.