Results 1 to 4 of 4

Thread: what's the correct way to share a hashtable in multiple threads?

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2004
    Posts
    362

    Smile what's the correct way to share a hashtable in multiple threads?

    I have a program that are multiple thread. Each thread calculate some data independently. however, they will need share a hashtable which controls a hardware resource. because the resource is limited, so, it may need lock the resource for a thread, and then release it when thread is finished. and the resource can not be reused immediately, will have to wait for some time, say 5 or 10 minutes, for the device to reset.

    obviously, use a global hashtable, it will work. is there any other suggestions? so, it looks more professional?


    thanks

    bear

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,109

    Re: what's the correct way to share a hashtable in multiple threads?

    Actually, I was particularly struck by the bit about "wait for some time". You have multiple threads that need to get at this hashtable, but it sounds like that isn't sufficient. It sounds like they actually have to get at a process, and the hashtable is almost secondary to the process. Would it make sense to allow something to get to the hashtable if the device has not reset? It doesn't sound like it from your description.

    If that is the case, I wouldn't worry about the hashtable. I'd make a process, which might be it's own thread, which takes whatever is needed as arguments, entirely wraps the hashtable (not global at all), and doesn't release until the device is ready for the next installment. I'd be controlling access to that process, not access to the hashtable.

    Of course, the alternative would be to have each thread lock the hashtable, work with the hashtable, and not release the lock until after the device has reset.

    The difference is whether one object (the hashtable) is used as a signal of whether any other thread can access the device, or whether the device access is controlled through a critical section of code that will only admit threads one at a time.
    My usual boring signature: Nothing

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2004
    Posts
    362

    Re: what's the correct way to share a hashtable in multiple threads?

    thanks for replying. here is what I'm doing:

    I have a control thread CT. in CT, it controls the multi-thread processing. say, totally, it has 10 threads for processing. when 10 threads are being used, CT is just wait. when a thread finished, CT starts a new one. each processing caluclates data for a specific type data.

    In each processing thread, it will need access an array of device. all processing threads need access the same device array although they may for different types of data. each device in the array can only be access once within a certain time.

    the hashtable uses the last access time as the value, the key is the name of the device. this way, I can track it.

    the thread control part & multi thread part work fine. Now, I only need add this device control (hashtable, or other better method). however, it means this HT needs be accessed by all processing threads. a global HT will work. But, I'm just thinking maybe it has a more "proefssional" way to do it.

    maybe a shared HT in a class? something like.

    thanks

  4. #4
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,109

    Re: what's the correct way to share a hashtable in multiple threads?

    I think you are doing fine. The design sounds good and it sounds flexible. If you are working in .NET, you don't have a global HT. At best, you have it in a module, but a module is nothing but a class with all shared members (in other words, you are doing EXACTLY what you suggested doing with it). I wouldn't change it away from that unless you have a specific reason to do so.

    If the global nature of the HT bothers you at some emotional level, then you could consider putting it into a Singleton class that kept it as a private member and exposed access to it through a set of properties and methods. I see no particular advantage to doing this, though, so I wouldn't do it.
    My usual boring signature: Nothing

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