dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: Concurrency Implementation issue!!!

  1. #1
    Join Date
    Apr 2004
    Posts
    123

    Concurrency Implementation issue!!!

    Hi,

    Iím looking out for an opinion on how to do this better..

    I have a shared dictionary between two threadsÖone thread is continuously updating it, say the updation is more than 20 times per second(financial market data) and another thread(timer) reads from the dictionary and updates a datatable which is binded to a grid.

    Since itís shared access I know I have to lock the dictionary, issue is which lock..I cant use the for example, lock(hybridDict.SyncRoot) because the writer thread should not be blocked,as itís critical that data which flows in should be the latest and accurate and in the next read cycle I should pick the latest updated value.

    Forexample if I say in thread1:
    Code:
    lock(hybridDict.SyncRoot)
    {
    //Read Dicitonary and put the latest value into datattable
    }
    And in thread2:
    Code:
    lock(hybridDict.SyncRoot)
    {
    //Update the dictionary,or specifically the values.
    }
    Iíll be running into the issue mentioned above if I implement like aboveÖ

    Now another complexity is thread2 is not actually a thread but a callback method which gets invoked regularly(so multiple writer threads) and also thread1 is a timer thread which gets invoked regularly(multiple reader threads too).

    What is the efficient method to implement this algorithm???

    Thanks in advance,
    mmx

  2. #2
    Arjay's Avatar
    Arjay is online now Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,209

    Re: Concurrency Implementation issue!!!

    When you lock, the blocked thread has to wait - there's no getting around that fact. So you need to minimize the amount of time the writing thread is blocked.

    Given this (and I really don't know how large the data is), I would use two containers. The dictionary that you have (I am assuming it's important for you to use a dictionary) and a queue.

    Something like Writing thread( callback ) -> queue -> dictionary.

    The writing thread pushes items onto the queue and triggers and event. The queue is serviced by a queue service thread which wakes up on the event and removes the items and puts them on the dictionary. This allows items to be received without interruption while the dictionary is being read.

    If you have multiples thread reading the dictionary (but I don't believe you do), you can use a ReaderWriterLock for synchronizing access to the dictionary. However, I'm not sure at these update speeds, you'll see a speed improvement (but you can test it out).

  3. #3
    Join Date
    May 2007
    Posts
    1,546

    Re: Concurrency Implementation issue!!!

    Unless the dictionary is updating tens of thousands (or probably hundreds of thousands) of times a second, you don't need to worry about which is faster. Just go with the method which is more maintainable.

    I'd say you'd be perfectly fine with just using a standard lock on the dictionary for both readers and writers. It'd be easy enough to create a small console application which spawns a load of threads which add/remove stuff from a dictionary. Then you can see how the app would perform.
    www.monotorrent.com For all your .NET bittorrent needs

    NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)