c# Iterate and do operations on a generic collection concurrently
pI've created a game emulation program using c# async socks. I need to
remove/add amp; do iterations on a collection (a list that holds clients)
concurrently. I am currently using lock, however, it's a a huge
performance drop. I also do not want to use local lists/copies to keep the
list up-to-date. I've heard about ConcurrentBags, however, I am not sure
how thread safe they are for iterations (for instance if a thread removes
an element from the list while another thread is doing an iteration on
it!?)./p pWhat do you suggest? /p pstrongEdit: here is a situation/strong
this is when a packet is sent to all the users in a room/p precodelock
(parent.gameClientList) { for (int i = 0; i lt;=
parent.gameClientList.Count() - 1; i++) if
(parent.gameClientList[i].zoneId == zoneId)
parent.gameClientList[i].SendXt(packetElements); //if room matches -
SendXt sends a packet } /code/pre pWhen a new client connects /p precode
Client connectedClient = new Client(socket, this); lock (gameClientList) {
gameClientList.Add(connectedClient); } /code/pre pSame case when a client
disconnects./p pI am asking for a better alternative (performance-wise)
because the locks slow down everything./p
No comments:
Post a Comment