Click to See Complete Forum and Search --> : Concurrency Implementation issue!!!


mmx_nexus
June 17th, 2008, 05:12 AM
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:

lock(hybridDict.SyncRoot)
{
//Read Dicitonary and put the latest value into datattable
}

And in thread2:

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

Arjay
June 17th, 2008, 01:50 PM
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).

Mutant_Fruit
June 17th, 2008, 02:45 PM
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.