Note that there are some explanatory texts on larger screens.

plurals
  1. POMultiThread in Games sharing resources, Need some feedback on a idea
    text
    copied!<p>I recently started with a project of making a game (I'm kinda new) and I started thinking about how you can implement multi threads to boost the performance. </p> <p>Suppose that you have a unit in a game and it has a position <code>x</code> and <code>y</code>. This position is updated from the internet by one tread, and another thread is using <code>x,y</code> to render the graphics of the unit (it has to know where it it).</p> <p>Now say that you put a mutex, or sephamore (a bit unsure which one is the best to use) on these variables. The problem is of course for the rendering thread. You can't stop and wait, the game would become laggy. This is not a problem for the internet thread though. Unless it's something wrong, a couple of more milliseconds wont matter to update the game. </p> <p>So I was thinking of how one could solve this problem and I got an idea. Say you create 2 sets of <code>x,y</code> (from now on lets only use <code>x</code> to make it simpler, but you get the idea). So you have <code>x1</code> and <code>x2</code>.</p> <ul> <li>Now the internet thread only updated x1. </li> <li>The graphic threads uses x1 and then updates x2 to its current value.</li> </ul> <p>Now here the idea. If <code>x1</code> is in a lock from the internet thread, the graphics thread will just say</p> <blockquote> <p>"Hey, I cant wait. I will just go ahead and use <code>x2</code> since that is such a good approximated of the position." </p> </blockquote> <p>And it will do so until <code>x1</code> is free again. It will look something like this.</p> <pre><code>//Thread Graphics: if (x1 is not locked){ lock x1: use x1 unlock x1: x2=x1 }else{ use x2 } //Thread Internet: wait until x1 is unlocked: lock x1: save data to x1 unlock x1: </code></pre> <p>Now I realise that this will take up some extra memory, but I think its worth it, at least if you limit your use of this technique to only critical pieces of data.</p> <p>So my Question is: what do you guys think of this idea ? Maybe it's already a common technique, only I don't know the name of it. If you have any other feedback on how to solve problems like this I would appreciate it. I think this is a common problem for most programmers.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload