Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Threadsafe Generic Dictionary

Threadsafe Generic Dictionary

Scheduled Pinned Locked Moved C#
question
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • W Offline
    W Offline
    Werdna
    wrote on last edited by
    #1

    What is the best way to create thread safe generic dictionary. The only operations done by multiple threads are check if key exists, get value and add value. I created a helper class that calls those methods internally, and uses lock inside the method. Is there a better way of doing it? I know in non-generic versions you could do Hashtable.Synchronized, but generic dictionary doesn't offer this.

    J 1 Reply Last reply
    0
    • W Werdna

      What is the best way to create thread safe generic dictionary. The only operations done by multiple threads are check if key exists, get value and add value. I created a helper class that calls those methods internally, and uses lock inside the method. Is there a better way of doing it? I know in non-generic versions you could do Hashtable.Synchronized, but generic dictionary doesn't offer this.

      J Offline
      J Offline
      Judah Gabriel Himango
      wrote on last edited by
      #2

      A "better" way of doing this would to use the ReadWriterLock, allowing multiple readers but single writers. And yeah, you'll need to implement all the methods yourself. The problem with the built in .Synchronized versions of Hashtable and ArrayList is that naive developers assumed that you could create a synchronized version of the list, have one thread insert an item in the list, and have another thread iterate over it using foreach. BAD PRACTICE! Because using a foreach will iterate over the list without locking it (it can't lock it because it doesn't know when the consumer is done iterating over it). So the insert on another thread interrupts and foreach, causing an exception because the list can't be modified while iterating over the list with foreach. In other words, you can't just assume a synchronized version of your list does takes care of all threading issues. The consumers of the list have to ensure proper synchronization.

      Tech, life, family, faith: Give me a visit. I'm currently blogging about: Homosexuality in Christianity Judah Himango

      W 1 Reply Last reply
      0
      • J Judah Gabriel Himango

        A "better" way of doing this would to use the ReadWriterLock, allowing multiple readers but single writers. And yeah, you'll need to implement all the methods yourself. The problem with the built in .Synchronized versions of Hashtable and ArrayList is that naive developers assumed that you could create a synchronized version of the list, have one thread insert an item in the list, and have another thread iterate over it using foreach. BAD PRACTICE! Because using a foreach will iterate over the list without locking it (it can't lock it because it doesn't know when the consumer is done iterating over it). So the insert on another thread interrupts and foreach, causing an exception because the list can't be modified while iterating over the list with foreach. In other words, you can't just assume a synchronized version of your list does takes care of all threading issues. The consumers of the list have to ensure proper synchronization.

        Tech, life, family, faith: Give me a visit. I'm currently blogging about: Homosexuality in Christianity Judah Himango

        W Offline
        W Offline
        Werdna
        wrote on last edited by
        #3

        Thanks for suggesting ReaderWriterLock, I already looked into it before and it seemed what I wanted, but using lock was just easier. I also knew about iteration problem, but I do not worry about that as I only need to insert and get data back.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups