# Is there a common way to execute a task at a certain pace, without relying on the task priority feature of the OS?

There is no cross platform way to set process priorities, and I don't want to use threads since there are no standard method of setting priority to certain threads, apart from the fact that threads are hard to use.

Let's say I have a task that takes a certain amount of computing time, which on my computer takes 10 seconds. This task usually takes a long time, like for example, loading geometry chunk dynamically, or opening a new audio ambience file. I don't need this task to be finished instantly because it's a big task so I can't afford to wait for a frame to finish, so I need to execute it over several frames, rather a certain amount of time.

Since I don't want to freeze the game, I can cut this loop into several batches and execute one batch per frame. If my game runs at 50 fps, a 10 second task can be divided in 500 batches. If my program loops over N*500 iterations, each batch loops over N iterations. If I don't want it impact others parts of my game, I can execute N/2 or even N/5 iterations per batch.

I'm not sure if I'm reinventing the wheel though, isn't there some C++ standard thing or programming pattern that allows me to do that already ? I'm not sure if it's optimal to cut a loop, and I'm not planning to use multithreading.

Despite having to deal with more issues (synchronization, memory barriers, and thread-local memory, etc) multithreading really is the easiest way to deal with this, and will yield the best performance in all likelihood. You can abstract away the platform-dependent code using a library like boost::thread or std::thread if you're using a C++11 compiler. You can deal with single-core processors by calling std::this_thread::yield() every so often in your low-priority tasks, or the equivalent in whatever library you use. Windows will typically give a running thread up to 10ms (most of a frame!) before forcing a context switch (if there are other waiting threads), but calling yield() is like saying "Okay, I know I've got more time, but I'm good for now, anyone else?".