Forum C# Multi-threading errors?

Multi-threading errors?

Postby robscherer123 » August 8th, 2014, 9:12 pm

Hello! I've got another question that I've had almost since I've begun making my game over a year and a half ago. I've recently moved from using arrays to lists. Nonetheless, I'm having close to the same problem as I've had with arrays. The problem is now the error "Collection was modified; enumeration operation may not execute", and well as sometimes the error "Object reference not set to an instance of an object".

I am getting the "collection was modified" error in my foreach loop, and the "object reference" error in my forloops. I understand both errors. The collection error is fired whenever a list is modified inside a foreach loop and the objectect reference error is fired when you try to access a property or method of a null object. I understand that. The problem is that no where in the foreach loop do I ever modify the collection or remove/add from it.

In the below example, enemy array would be a List<Enemy>. So basically my code would look like this inside my GotMessage function:

Code: Select all
case "damageEnemy":
     foreach(Enemy _enemy in enemy_array){ //this lines throws the error
          _enemy.health -= 10;
     }
     break;


But for some reason I am getting an error with that telling me that that the collection was modified. The only time I ever remove or add to the enemy_array is NOT inside the foreach loop, its inside my main game loop. So how is it possible for these things to be going null or getting removed mid-iteration? Are the GotMessage and game loops executing simultaneously?

I've learned most of my programming from AS3, so I am pretty unfamiliar with multi-threading. Is it possible that the lists/arrays are getting edited elsewhere during the iteration by another thread? I've saw on google that I can use an expression called "lock" to try and prevent this. I just thought I'd get some feedback here first, as I've always been lost as to why I get this error in these places.
robscherer123
Paid Member
 
Posts: 313
Joined: December 12th, 2012, 8:46 pm

Re: Multi-threading errors?

Postby Henrik » August 8th, 2014, 9:19 pm

robscherer123 wrote:I've learned most of my programming from AS3, so I am pretty unfamiliar with multi-threading. Is it possible that the lists/arrays are getting edited elsewhere during the iteration by another thread? I've saw on google that I can use an expression called "lock" to try and prevent this. I just thought I'd get some feedback here first, as I've always been lost as to why I get this error in these places.


Yes, this is exactly what's happening. If you're getting multiple messages of the type "damageEnemy", that code will run multiple times concurrently, not one after the other.

The simple fix is to put a lock around the list that you're iterating over to ensure only one thread at a time does it. The drawbacks of that is that every other thread is going to block and wait for the lock, which is generally bad.

A better solution would be to for example queue up messages in your client, so that you never fire another "damageEnemy" message until you've gotten a reply back from the one you just fired.

Yet another solution would be to go over all of your server-side code and make it multithread-aware, i.e. think about what happens when you get multiple messages at once, and see if you can make that work anyway, i.e. make sure all changes to shared variables are done atomically.
Henrik
.IO
 
Posts: 1880
Joined: January 4th, 2010, 1:53 pm

Re: Multi-threading errors?

Postby robscherer123 » August 8th, 2014, 9:42 pm

Ok thanks for the reply. I've been confused about this for a while, and its something new to me since AS3 generally does everything one step at a time. I'll have to read up about it and get more acquainted with multi-threading and everything. I'll probably just throw a lock around it for now until I become more familiar with it. Thanks for the help!
robscherer123
Paid Member
 
Posts: 313
Joined: December 12th, 2012, 8:46 pm

Re: Multi-threading errors?

Postby Pawel Wozniak » October 25th, 2015, 1:10 am

Henrik wrote:
A better solution would be to for example queue up messages in your client, so that you never fire another "damageEnemy" message until you've gotten a reply back from the one you just fired.


Is locking all GotMessage section with lock() a good idea? What else I can do to queue messages? In my case there are max 2 players in room simultaunously each sending 5-6 messages per minute.

Btw. I have several properties in Player class that I want to lock can i use lock in getter/setter functions are somehow make then multithread-proof :) ?
Pawel Wozniak
Paid Member
 
Posts: 96
Joined: October 14th, 2012, 10:47 pm


Return to C#



cron