Multithreaded Hastable Access

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ajay

    #1

    Multithreaded Hastable Access

    I have a global hashtable instance in my application which is accessed by
    threads within that application for readonly access. Do I need to synchronize
    the hashtable if readonly access is required? Will the multiple threads be
    able to search the hastable simultaneously ?

    Thanks

    Ajay
  • Jon Skeet [C# MVP]

    #2
    Re: Multithreaded Hastable Access

    Ajay <Ajay@discussio ns.microsoft.co m> wrote:[color=blue]
    > I have a global hashtable instance in my application which is accessed by
    > threads within that application for readonly access. Do I need to synchronize
    > the hashtable if readonly access is required? Will the multiple threads be
    > able to search the hastable simultaneously ?[/color]

    Yes, that's fine. You're okay to have one writer and several readers,
    too, so long as none of them try to iterate through the hashtable while
    the writer is writing.

    --
    Jon Skeet - <skeet@pobox.co m>
    Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

    If replying to the group, please do not mail me too

    Comment

    • Sijin Joseph

      #3
      Re: Multithreaded Hastable Access

      No need to synchronize if the hashtable is readonly. Otherwise you can
      use Hashtable.Synch ronized() to get a thread safe instance of the hashtable.

      Sijin Joseph





      Ajay wrote:[color=blue]
      > I have a global hashtable instance in my application which is accessed by
      > threads within that application for readonly access. Do I need to synchronize
      > the hashtable if readonly access is required? Will the multiple threads be
      > able to search the hastable simultaneously ?
      >
      > Thanks
      >
      > Ajay[/color]

      Comment

      • Leon Lambert

        #4
        Re: Multithreaded Hastable Access

        Personally i would to be safe. You say it is read only but someone has
        to fill it in at some point and, as projects seem to go, feature creep
        at some point may dictated modifications. I would use a
        ReaderWriterLoc k. This is very nice for items like this. You can have
        many readers accessing the data as long as no one is changing it but
        feel safe if the collection eventually needs to be changed. At a minimum
        I would make sure to wrap the collection up in a manager object that all
        accessors use to get at the data. This way you could always add the
        ReaderWriterLoc k later if needed without having to touch all the other
        software that needs access to it.

        Hope this helps
        Leon Lambert

        Ajay wrote:[color=blue]
        > I have a global hashtable instance in my application which is accessed by
        > threads within that application for readonly access. Do I need to synchronize
        > the hashtable if readonly access is required? Will the multiple threads be
        > able to search the hastable simultaneously ?
        >
        > Thanks
        >
        > Ajay[/color]

        Comment

        Working...