Results 1 to 2 of 2

Thread: Thread-safe sharing of a List between multiple clients

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Sep 2011
    Posts
    27

    Question Thread-safe sharing of a List between multiple clients

    I am trying to find out what would be the best approach to the following problem:

    I have a TCP Server handling multiple clients. They constantly hit the server (send and receive messages).

    In the current implementation each client is in its own thread. Within that thread each client checks with database for new messages.

    Instead of each thread calling Oracle on its own I would like to create a special thread holding a list of messages, so all of the connected clients could check that list instead.

    The issue is that once I will modify the thread (remove from it) then it is no longer thread-safe.

    After googling a little bit I found I will need to lock all of threads and then do the necessary modifications to the list. Is that the best solution? It looks like introducing a delay to the server performance.

    Thanks for your help!

  2. #2
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    9,017

    Re: Thread-safe sharing of a List between multiple clients

    Then you should do your best to optimize the code that modifies the list. You're worried about performance but locking the operations on the list is far more important. Without that synchronization, you risk threads reading invalid data because another thread is smack in the middle of making changes to it. This could prove far more disastrous than any performance issues.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

Tags for this Thread

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