Results 1 to 3 of 3

Thread: Need the Threading Gurus Please!!

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2002
    Posts
    382

    Need the Threading Gurus Please!!

    Might anyone out here help me by answering a few threading questions that I can't seem to find anywhere. My question deals with Dictionary Objects (Array List, HashTable, Collections) and reading, writing, and iterating through them safely in different threads. Ok here is what the documentation says about Hashtables and threading...

    To support one or more writers, all operations on the Hashtable must be done through the wrapper returned by the Synchronized method.

    Enumerating through a collection is intrinsically not a thread-safe procedure. Even when a collection is synchronized, other threads could still modify the collection, which causes the enumerator to throw an exception. To guarantee thread safety during enumeration, you can either lock the collection during the entire enumeration or catch the exceptions resulting from changes made by other threads.
    So the first part of my question is.. Is this a safe way to declare a syncronized wrapped hashtable?

    VB Code:
    1. 'This is declared in the scope of a Class
    2. Private Accounts As Hashtable = Hashtable.Synchronized(New Hashtable)

    Or do I need to create a normal Hashtable in the Class's Global scope then declare a new sycnronized wrapper in each method I call that works with the data in the hashtable?


    Next Question...


    The docs clearly state that you shouldn't enumerate through a hashtable even if its wrapped in a syncronized wrapper without specifically Locking the iteration first.. So My question is..

    If I'm locking the Hash Table during Iteration like so in one thread:

    VB Code:
    1. SyncLock Accounts.SyncRoot
    2.             For Each a As Account In Accounts
    3.                 ...Code Here...
    4.             Next
    5.         End SyncLock

    But a method in another thread is adding a object at the same time like so:

    VB Code:
    1. Accounts.Add(AcctID, Acct)

    Wouldn't I need BOTH methods to have the hashtable SyncLocked to be thread safe? Or will that work with JUST locking during enumerating and letting the syncronized wrapper handle syncronizing during adds and removes?

    It just seems to me that SyncLocks work in conjunction with one another. So if I have the hashtable locked during Enumeration in one method, I can still access it, read from it, write to it, and remove from it on another method so long as I don't lock it in the second method. This will cause the first one to error out even though it's wrapped and locked...

    So what's the point of even wrapping the hashtable in a syncronized wrapper if I'm going to have to end up SyncLocking every line of code that manipulates the data in the hashtable??

    I know that this probably doesn't make a whole bunch of sense if you aren't famailiar with it but any help would be appretiated


  2. #2
    Hyperactive Member
    Join Date
    Jan 2002
    Location
    Palermo, Italy
    Posts
    325
    I can answer the second question.

    In my application I use the synclock statement to lock a collection before the enumeration. I noticed that other threads that want to access that object stop until the object is unlocked.

    But I want to go more in deep in this stuff... Maybe later I can give you a sure answer.

    Xmas79
    Learn, this is the Keyword...

  3. #3
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    A Hashtable can safely support one writer and multiple readers concurrently.
    To support multiple writers, all operations must be done through the wrapper returned by the Synchronized method.
    -MSDN

Posting Permissions

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



Click Here to Expand Forum to Full Width