Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 658267
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T22:54:52+00:00 2026-05-13T22:54:52+00:00

I’m having issues with concurrent usage of a shared collection with a multi-player synchronous

  • 0

I’m having issues with concurrent usage of a shared collection with a multi-player synchronous game I’m working on. I did some digging around and found a neat thread-safe IEnumerator/IList implementation on Alexey Drobyshevsky’s post on codeproject here:

http://www.codeproject.com/KB/cs/safe_enumerable.aspx

After adopting his implementation I have even replaced all Linq queries on the shared collection with for/foreach loops because the Linq queries were still using the unsafe IEnumerable underneath.

Here’s my implementation of the SafeList, and the list itself is being exposed to as a ReadOnlyCollection to the consuming classes.

http://theburningmonk.com/2010/03/thread-safe-enumeration-in-csharp/

After switching to this SafeList I am seeing far less problems, but under heavy load (80+ threads all of which read/write from and to the list at different points) I’m still seeing InvalidOperationException being thrown:

The element list has changed. The enumeration operation failed to continue

I have even tried using ReadWriterLockSlim in place of the lock object in my implementation of the SafeList but that proved fruitless too. The only other suggestion I have had so far is cloning the list every time a threads needs to loop through it. I’m hoping to avoid cloning the list every single time as the list is being used in way too many places it might be a performance hit and might introduce other bugs that are difficult to spot.

Given the time constraints I’ll have to be pragmatic about it and if cloning is the safest and quickest way to go about solving this problem then I’m fine with it, but before resorting to this last ditch-attempt I’m just wondering if anyone out there has come across something similar is able to offer some advice.

Many thanks in advance!

[EDIT] Here is a little more information about the problem I’m seeing as requested:

For one ‘game’, there can be up to 100 or so synchronous clients connected and the game needs to message each connected client with updates every few seconds and so every few seconds this game needs to iterate through the shared list of players.
When a player joins, or leaves, the list needs to be updated accordingly to reflect the changes.
To add to that, the players can interact with the game and chat to other players, and every time a message is received from the player the game would again need to iterate through the same list and do a broadcast.
The exceptions are typically thrown when the game is trying to broadcast messages to players (read operation) at the same time as many players leaving/joining (write operation) at the same time.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T22:54:52+00:00Added an answer on May 13, 2026 at 10:54 pm

    Given your description of the game structure, considering having a single thread that is the only thread that can directly access the list of players. Make the list effectively private to that one thread.

    The way any other threads access the list is by sending messages to the list-manager thread. So that thread has a queue of messages that it waits on. While the queue is non-empty, it chews through the messages, following their instructions. They might say “Add a new player”, or “Remove a player”, or “Update the status of this player to ‘unhappy'”.

    This thread can also periodically scan the list to send updates out to clients, or it can (even better) use the fact that it has a known stream of changes that are occurring to the list, and forward just those changes on to the clients.

    Basic principle: make data private to one thread, and have the threads communicate via message queues.

    Your basic data structure is a thread-safe queue class. There are dozens of examples on SO already. (And steer clear of any that claim to be “lock free” and yet thread safe. It’s just not worth the risk.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
I have some data like this: 1 2 3 4 5 9 2 6
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.