Results 1 to 8 of 8

Thread: Multithreading and enumerating...

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jan 2006
    Posts
    31

    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

  2. #2
    Fanatic Member MetalKid's Avatar
    Join Date
    Aug 2005
    Location
    Green Bay, Wisconsin
    Posts
    534

    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.

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Jan 2006
    Posts
    31

    Re: Multithreading and enumerating...

    Where do I throw the SyncLock around? Commands that are adding/removing from a list? Enumerating the list? Or both?

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Jan 2006
    Posts
    31

    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.

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Multithreading and enumerating...

    Have you read the help topic for the Dictionary class? What does it say?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Jan 2006
    Posts
    31

    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.

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Multithreading and enumerating...

    Quote 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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width