   XNA Multithreading
  Alright so I'm pretty new with mutlithreading and have a couple of problems. Basically what I have is my main game loop in xna, with its Update and Draw.In the main loop i have a list of planets, the list I want to start off with no planets etc. But what I want to be able to do is say add a new planet to that list every 5 seconds etc, so I want to have some seperate worker threads that create a planet and push it back onto my main list. Only problem is when I've tried this I end up crashing something.What I've tried is something in the lines of this.XNA's default game loop, I've changed nothing in here. It's as if it's just the default.Init()    new PlanetList...LoopUpdate()    foreach planet -> Update();Draw()    foreach planet -> Draw();What I want to be able to do is in the update have something like:Update()if(timer > 5 seconds) // this part is really irrelevant but for the sakes of this example    ThreadStart = new ThreadStart(ThreadJobPlanet)    Thread = new Thread()    ThreadStart()foreach planet -> Update()I tried making a basic thread job like this:protected void ThreadjobPlanet(){    Node planet;    planet = new Planet();    planet.CreateNode(device, Content);    planet.Position = new Vector3(Global.Instance.rand.Next(-50000, 50000),    Global.Instance.rand.Next(-50000, 50000),    Global.Instance.rand.Next(-50000, 50000));    planets.Add(planet);}But every now and then I'll get a crash, which I gather is from something like both of them accessing the planets list at once?I've really no clue on how I can do this, but I thought something like this would be really simple. So any help on this would be greatly appreciated :) Hopefully what I'm trying to explain makes sense. I just want to have a few worker threads here and there that keep adding to my ever expanding universe.   
  Yeah. For starters, you need a mutex around any shared state that can be written/read by multiple threads.  That means locking the planet list before adding a new planet inside the thread, then unlocking it after. You also have to lock the planet list before you iterate over it for updating, and unlock it when you are done.Secondly... is your planet creation that cpu intensive that you need the process in another thread?  Even if it is, you don't want to be spawning threads all the time for this. You want to create one persistant thread, and have it just go to sleep when it doesn't have work to do.;  It takes about 2 - 3 seconds for a planet to be generated. I have a few things that go on behind the scene the are decently optimized, Could you possibly point me in the right direction on how I could go with something like this. In terms of I know nothing of how a mutex works in xna. So I'm not sure how I'm meant to be wrapping anything up at all. I've tried looking for tutorials but a lot of them seem to be nothing to do with what I need with the end goal.Thanks for explaining the process I'll need though.   ;  C# has built-in language support for mutexes. Simply surround your code that you want to protect with lock().   ;  You might want to also consider using the ThreadPool class instead of constantly creating new threads by hand. And as glaeken suggested, put all the code that accesses the planets list inside a 'lock' statement. Note that the actual planet creation shouldn't be inside the lock (as this will kill your performance) but just the line that adds the new planet to the list.Here's what your code might look like:// Update():if(timer > 5 seconds) {    ThreadPool.QueueUserWorkItem(ThreadJobPlanet);}lock(planets){    foreach planet -> Update()}   // Similar for Draw, lock the list before iterating over it.protected void ThreadjobPlanet(){    Node planet;    planet = new Planet();    planet.CreateNode(device, Content);    planet.Position = new Vector3(Global.Instance.rand.Next(-50000, 50000),    Global.Instance.rand.Next(-50000, 50000),    Global.Instance.rand.Next(-50000, 50000));    lock(planets)    {   planets.Add(planet);    }};  Thank you doesnotcompute, that was exactly what I figured out last night pretty much. Just nice to see someone else do it so I know I'm on the right track. Thank you guys :)   