Welcome, Guest. Please login or register.

Login with username, password and session length

 
Advanced search

1411512 Posts in 69376 Topics- by 58431 Members - Latest Member: Bohdan_Zoshchenko

April 27, 2024, 07:52:00 AM

Need hosting? Check out Digital Ocean
(more details in this thread)
TIGSource ForumsDeveloperTechnical (Moderator: ThemsAllTook)implementation of threads in sync with idle time in main loop
Pages: [1]
Print
Author Topic: implementation of threads in sync with idle time in main loop  (Read 1185 times)
cmspice
Level 1
*


View Profile
« on: December 25, 2009, 12:28:39 AM »

So here is the situation, lets say I have either a game with some fairly large assets or a fairly large gameworld. To reduce loading times I want to preload assets for unvisited areas. Of course I could do this all at once on initializing the game but it's much smarter to do this during the idle times of my main loop.

Being not the greatest expert on threads, I'm really not too sure how they prioritize threads. But is there any way to properly activate a thread for a given amount of time? E.G. at end of main loop I know I have 10 ms to spare so I want to run the asset loading thread for 10 ms before switching back. Is this there some way to do this or am I approaching this problem in totally the wrong way??
Logged
mcc
Level 10
*****


glitch


View Profile WWW
« Reply #1 on: December 25, 2009, 01:04:12 AM »

Normally if you're using threads it's to avoid situations like having to partition out timeslices in an "okay, I have 10 ms, go" sense...

The normal thing to do in this case would be to say usleep(n*1000) (or Sleep(n) on Windows) where n is the number of milliseconds you know you'll be idle for. What this does is that your thread surrenders for the specified number of milliseconds, and requests to be woken back up after that. During this time your background thread will be free to work. Of course, if you do it this way your background thread will just run all the time and thus will also be running while you're not idling. But this is okay. It's probably best to just trust the OS to hand out timeslices, especially if you're new at threading, unless you have specific reason to believe doing otherwise is causing performance issues.
« Last Edit: December 25, 2009, 01:51:13 AM by mcc » Logged

My projects:<br />Games: Jumpman Retro-futuristic platforming iJumpman iPhone version Drumcircle PC+smartphone music toy<br />More: RUN HELLO
kometbomb
Level 0
***


View Profile WWW
« Reply #2 on: December 25, 2009, 08:58:12 AM »

Yeah, threading essentially is automated slicing. How about simply setting the thread priority with SetThreadPriority() (Windows)? I don't know how to prioritize gfx stuff since a lot of the time spent in a loader thread will be waiting for textures etc. being sent to the GPU.

On an image viewer I'm developing I notice no slowdown when uploading textures in the background and drawing the loaded textures in the main thread. So, I wouldn't worry about thread priorities. Premature optimization etc. Just make sure the thread idles properly when not doing anything, e.g. with a Sleep(1) or so.
Logged

cmspice
Level 1
*


View Profile
« Reply #3 on: December 25, 2009, 09:17:43 AM »

Okay, cool thanks.  I guess that makes my life a lot easier.  I'm such a control freak in my code sometimes Smiley.
Logged
Pages: [1]
Print
Jump to:  

Theme orange-lt created by panic