Jump to content
  • Advertisement
Sign in to follow this  
Nairou

Unity Latency vs Speed: Multithreading or single-threading

This topic is 5076 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm currently working on the core design of my game engine, deciding how the components will fit together. My current concerns are keeping input latency low, and making sure video v-sync doesn't stall the gamestate updates. After reading through these forums and other articles over the past week, I've been toying with two ideas: IDEA 1 (from this thread) Use a single thread, and attempt to run the game state (including input) at a fixed rate, while allowing the renderer to run at full speed. Essentially the game loop runs as fast as possible, and within each iteration the time is checked to determine if the game state should be updated, which could be set to around 20 times a second. PROBLEM As nice and simple as this method is, I see two big problems. First, input latency. Polling for player input only 20 times a second isn't going to feel very smooth to the player, it will react rather choppy. And Second, the speed of the loop could stall if v-sync is on, and the renderer ends up waiting for large spaces of time. This could throw the game state off, and player input will be even more erratic. (And yes, I do want to be able to use v-sync.) IDEA 2 (triggered by this thread) I use multiple threads. The main thread controls the game state, and does a lot of sleeping, running only around 20 times a second. The renderer has its own thread, which runs as fast as possible, over and over. V-sync works just fine, since it will only stall that one thread, the main thread continues on as normal (and in fact benefits from the v-sync stalls by making better use of the CPU). Input as well has is own thread, and it too sleeps most of the time, waking up multiple times a second to poll for player input. PROBLEM The main problem here is the potential fight for CPU time. With the rendering thread running full speed, the other two threads will have to fight to get their updates done without causing game state or input stutters. One solution I read a lot involves messing with thread priorities, making the renderer a lower priority than the other two, so that they get a chance to run when they wake up. But this feels more like playing with fire to me, changing thread priorities and hoping it all works out. Unlike the single-thread method, where everything was linear and we knew for sure it worked as expected, I don't have as much faith in the thread priorities, and the various platforms the game may be ported to. And still, we have the issue of how often we need to poll for input, to balance between using too much CPU, and preventing input stutters from the delays. I believe DX has the option to use an event system for input, where the input thread is able to sleep until the player provides input, then it automatically wakes up to handle it. But I don't know if this is available for all forms of input (keyboard/mouse/joystick), and more importantly, I don't know that this is available on other platforms (heck, I don't know anything about game input under Linux right now). So, I feel stuck, there are pros and cons to both ideas, speed or latency issues either way. Anyone have some input on this, your own experiences or alternatives? Anything would be appreciated, I'm ready to move on from here. :)

Share this post


Link to post
Share on other sites
Advertisement
The rule is quite simple in the end: Thread overhead is not worth it, only thread what you must. Basically what you have right now sounds like a sound idea, if you want a thread to take up less of the timeslice you can have it check how long it's busy with GetTickCount and call Sleep(0) when it's exceeded it's slice according to you.

You could also have threads call longer Sleeps.

Share this post


Link to post
Share on other sites
Honestly, from my experiences, you only really want to mess with a few threads:

1.) A thread for rendering. This should only do rendering. You might want to just make it your main thread for simplicity's sake.

2.) A thread that handles AI, input, physics, and all other CPU-dependent stuff. Don't do a seperate thread for input; it's unnecessary and will probably cause you to get input lag.

3.) A thread for music and sound. Duh.

4.) A thread for networking. You MIGHT be able to put this one into the main thread, or the CPU-dependent thread, but it depends on your overall design.

5.) A thread for streaming in data. Duh.

Share this post


Link to post
Share on other sites
Buth when it comes to multithreading, is there an alternative to modifying thread priorities? Or maybe theres something I don't know about how threads cooperate? Making them sleep as much as possible is the best thing I can think of, but it still seems that they'll be competing a lot.

Etnu, the threads you have listed there sound good, though I know from your other posts that you're able to keep input in the main thread because you run it 100 times a second. This seems a little extreme to me, surely that creates some heavy CPU competition with the renderer thread? And given that, in a multiplayer game, the host is only broadcasting updates 10-20 times a second to the clients, it seemed redundant for all of the main loop to run faster than that. What are your thoughts there?

Share this post


Link to post
Share on other sites
1.) A good renderer should do as little as possible on the CPU. Your video card is powerful. Use it. Stop wasting time trying to make sure every little polygon that isn't visible isn't drawn. That's pointless. Use the graphics card's features. Occlusion queries. Lay down Z. The odds are that you're just wasting clocks with your culling algorithms if you're only pushing 5-10 million polys / sec.

2.) Priorities don't need to be modified much. Sleep(0) is usually good enough. Multi-tasking OSes like windows work remarkably well with seperate threads. Just check your processes tab...

3.)
Quote:

And given that, in a multiplayer game, the host is only broadcasting updates 10-20 times a second to the clients, it seemed redundant for all of the main loop to run faster than that.


Simple:


const float InputResolution = 0.01f;

float InputUpdateTimer = 0.0f;
while(true)
{
UpdateTimer += DeltaTime();
CheckForNetworkUpdates();
if(UpdateTimer >= InputResolution)
{
UpdateTimer = 0.0f;
QueryInput();
}
UpdatePhysicsStuff();
}



Share this post


Link to post
Share on other sites
Keep in mind here that the input engine is idle 90% of the time. Processing an action when you press the "up" key is not likely to eat many CPU resources.

Share this post


Link to post
Share on other sites
threads do not compete. They run during a given time slice (the quantum) and then return the control to the kernel which wake up another thread (and so on... only sleeping threads and threads which are in a "wait" state are not concerned).

If one of your thread do not have anything to do, then Sleep(0). It's easy and simple - it will only return the control to the OS which can then wake another thread.

Playing with thread proirity must be done only if you have a good understanding of what is done behind the scene. It will modify the way the OS allocates time quantums - and therefore may change the way your program is executed. This means that if your threads are communicating then you'll have to take care to the synchronisation of the threads.

Rising the priority of a thread will of course give it more time to execute - as a consequence, other threads will have less time. This is done by using the SetThreadPriority() function in Windows (see the msdn for further informations about this function and its parameters).

HTH

Share this post


Link to post
Share on other sites
It depends a lot on the OS, but the generally:

1. In a pre-emptive system (like Windows), threads of the same priority take turns. They switch after a small period of time or when the current thread relinquishes control.
2. In a cooperative system (like on a certain console), threads of the same priority take turns, however they switch only when the current thread relinquishes control.
3. Threads of a lower priority only run when no higher priority thread is ready to run.

Share this post


Link to post
Share on other sites
Quote:
Original post by Emmanuel Deloget
... Rising the priority of a thread will of course give it more time to execute ...


That is not accurate.

Example: two threads have the same priority. Since they take turns, each gets about 50% of the time. If you raise the priority of one, it will get 100% and the other will get 0%.

Another example: two threads have different priorities. The higher priority thread relinquishes time occasionally, so the lower priority thread get about 30% of the time. If you raise the priority of the lower thread, but it is still lower than the higher thread, nothing will change.

Of course, this is simplified and depends on the OS.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!