|
-
Oct 4th, 2006, 11:44 AM
#1
Thread Starter
Junior Member
Multithreading and enumerating...
Hey guys!
So I've been working on a socket server for a game for some time now. It has your typical instant messaging and chatting as well as racing. It's multithreaded as each socket connection is a different thread as well as a maintenance thread that works in the background to cleanup objects that are sitting around (namely stuff like race requests that were never accepted or declined).
The problem I'm running into is that I'm doing a lot of enumerating. Reason being is I have generic lists and dictionary objects of races, clients, chatrooms, etc. And they're constantly changing. So, just recently because of testing we had more users that normal on doing lots of operations and I received the following error here and there:
Collection was modified; enumeration operation may not execute.
Of course this was a pain to track because it happens when something is trying to add/remove something from a collection whilst the collection is being enumerated. After doing some reading, it looked like the best option was to use SyncLock, however, that would be a performance hog. I do recall reading somewhere just to make a copy of the reference object and enumerate through that. Would this be an ideal situation? Which is less of a performance hog? Is there a better option? I'm pretty far along in this application, and I know I'm going to have to make some changes wherever there is a For Each that has to do with objects that are being touched by other users because of this.
Please advise!
Thanks all,
-Steve
-
Oct 4th, 2006, 01:34 PM
#2
Fanatic Member
Re: Multithreading and enumerating...
Since you are using multiple threads, you'll have to use SyncLock. Even if you were making a copy, it is possible that another thread could access it in the middle of copying it. I don't think SyncLock is a peformance hog, but it could cause delays depending how often the object is accessed.
-
Oct 4th, 2006, 05:47 PM
#3
Thread Starter
Junior Member
Re: Multithreading and enumerating...
Where do I throw the SyncLock around? Commands that are adding/removing from a list? Enumerating the list? Or both?
-
Oct 4th, 2006, 06:16 PM
#4
Re: Multithreading and enumerating...
It's always a good idea to read to read the documentation for the classes you're using, particularly when things don't work as expected. This is from the help topic for the List(Of T) class:
Public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
A List can support multiple readers concurrently, as long as the collection is not modified. Enumerating through a collection is intrinsically not a thread-safe procedure. In the rare case where an enumeration contends with one or more write accesses, the only way to ensure thread safety is to lock the collection during the entire enumeration. To allow the collection to be accessed by multiple threads for reading and writing, you must implement your own synchronization.
That means that anywhere you enumerate the collection or write to the collection should be wrapped in a SyncLock block. I would use the collection itself as the lock object. If you don't know what I mean by "lock object" then you should read the help topic for the SyncLock statement.
-
Oct 4th, 2006, 06:44 PM
#5
Thread Starter
Junior Member
Re: Multithreading and enumerating...
Hmmm, I think List isn't thread-safe, but is Dictionary? If Dictionary is thread safe, then I shouldn't need to go in massive SyncLock throw in mode.
-
Oct 4th, 2006, 06:52 PM
#6
Re: Multithreading and enumerating...
Have you read the help topic for the Dictionary class? What does it say?
-
Oct 5th, 2006, 10:05 AM
#7
Thread Starter
Junior Member
Re: Multithreading and enumerating...
Help topic on MSDN or on the forums? I'm definitely new to these forums, and am not sure where the help topics are.
-
Oct 5th, 2006, 05:28 PM
#8
Re: Multithreading and enumerating...
 Originally Posted by LookitsPuck
Help topic on MSDN or on the forums? I'm definitely new to these forums, and am not sure where the help topics are.
Bingo. When you installed the IDE you should have installed the MSDN library with it. If you didn't I suggest that you fix that ASAP. You access help in VS the same way you do in any other Windows app. Having said that, everything that is available in the local MSDN library is available in the on-line version, plus much more besides.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|