Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

Until recently I was sure that running expensive operations in threads can prevent UI freezing, but now I'm not.

Is it not guaranteed that a threaded operation will not hog the main thread's resources? If not, then how can I run a method in a way that it will never ever drain resources from UI thread (until told to)?

share|improve this question
    
Threads should be fine for keeping the main thread responsive. What actual problem are you running into? –  Sean Middleditch Feb 28 '13 at 1:11
    
When running a resource consuming operation in a thread, some update frames get dropped in my XNA game. –  user1306322 Feb 28 '13 at 1:13
    
Unfortunately unless your computer has more processors than processes and threads you can't really guarantee that you will always hit every update frame. Each thread needs time on the processor and to do that it is going to have to pause your main thread for a few cycles every now and then. I'm not sure what your main time consuming problem is, but I suggest you find a good place to handle it (loading screen between levels) rather than push it into a thread (especially since the thread might not complete in time). –  Benjamin Danger Johnson Feb 28 '13 at 20:39

1 Answer 1

Is it not guaranteed that a threaded operation will not hog the main thread's resources?

You are running on a computer that's playing host to a preemptive multitasking Operation System. Nothing guarantees your thread resources. There is nothing you can do on a computer that will guarantee that your thread's computational resources won't get taken away. Either by your own threads or some other processes.

That's life in a preemptive multithreading environment. Best live with it. The only thing you can do is minimize the chance of something happen; you can't eliminate it.

Without knowing anything about your inter-thread communication system, or how much work you're doing in this other thread, or any of the particulars of your situation, no solution can be offered here.

share|improve this answer
    
Actually I was thinking of controlling all non-main threads in a way that will prevent them from stealing resources from the one and only game's main thread (which runs Update and Draw in a loop). Assuming any external process (not the game itself) can't be controlled, so I'm trying to understand if it's possible to do only with the game's threads. –  user1306322 Feb 28 '13 at 10:37
    
@user1306322: The "preemptive" part of "preemptive multitasking" means that you are guaranteed nothing. Even ignoring other processes, you're still guaranteed nothing about how many time-slices each thread gets. Even with only one thread, you're guaranteed nothing save that the OS will get around to it... eventually. There is nothing you can do in your other threads to make them give time to the main one. You can only encourage the OS to give time to your main thread (and even that is only by process of elimination, if all other threads are waiting on stuff). You can't force the OS. –  Nicol Bolas Feb 28 '13 at 10:44
    
@user1306322: You are going to have to write your main thread to be able to live with the very real possibility that, every now and then, it may not get to process a full frame. It may drop a frame or something through no fault of its own. It's going to have to be able to deal with that. It won't happen often, assuming adequate CPUs are available, but it can very much happen. –  Nicol Bolas Feb 28 '13 at 10:46
2  
You may also want to 1) throttle your worker thread so it isn't running full bore all the time if it really is sucking up that much CPU/IO/GPU etc, and 2) make sure your locks aren't causing contention. –  Patrick Hughes Feb 28 '13 at 16:07

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.