0

Alright i have an application which starts hundreds of crawling tasks at the same time and those tasks adds hundreds of found urls into a dictionary

I am using lock to add each found url however i see that all threads are waiting at lock (it works but i am looking for faster working way if there is any)

I wonder are there any better way for me to improve performance

The application is .net 4.5.1 c#-5 WPF application

This is my dictionary definition

   private static Dictionary<string, UrlHolder> dicUrlHolder = new Dictionary<string, UrlHolder>();

This is how i use

lock (dicUrlHolder)
        {
//do stuff here each time gets a lock
        }

So i wonder any other better way to use it between hundreds of threads locking concurrently hundreds of times to add / update

Furkan Gözükara
  • 22,964
  • 77
  • 205
  • 342
  • 6
    Use `ConcurrentDictionary` – Vsevolod Goloviznin Jan 15 '15 at 11:56
  • Like Vsevolod said ConcurrentDictionary would be good there. Answers about that can be seen here: http://stackoverflow.com/questions/2940241/making-dictionary-access-thread-safe (and in the question this one is a duplicate of) – Thomas Jan 15 '15 at 11:57
  • 1
    And of course: Inside the lock do nothing but modify the dictionary. All other statements before the lock – DrKoch Jan 15 '15 at 11:58
  • 1
    what exactly happens in `do stuff here each time gets a lock`? – ie. Jan 15 '15 at 12:03
  • yes i know ConcurrentDictionary but is it really faster ? this article is really interesting as @ie mentioned : http://www.codeproject.com/Articles/548406/Dictionary-plus-Locking-versus-ConcurrentDictionar i read it before as well – Furkan Gözükara Jan 15 '15 at 12:15
  • _"... that all threads are waiting at lock"_ - they shouldn't be. You have another problem where you don't expect it. Locking on a normal Dictionary should be fine, esp with so much I/O going on. – H H Jan 15 '15 at 12:42

1 Answers1

2

You can use ConcurrentDictionary<TKey, TValue> Class

http://msdn.microsoft.com/en-us/library/dd287191(v=vs.110).aspx

Giorgi Nakeuri
  • 35,155
  • 8
  • 47
  • 75
  • @MonsterMMORPG says that he has a performance issue here. Unfortunately, `ConcurrentDictionary` does not always help. See for example [this codeproject article](http://www.codeproject.com/Articles/548406/Dictionary-plus-Locking-versus-ConcurrentDictionar) – ie. Jan 15 '15 at 12:08