Re: Thread safety help on Hashtable

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



On Mar 3, 5:36 pm, mattd0878-use...@xxxxxxxxx wrote:
I'm sort of unclear on what is thread safe and what isn't when using
Hashtable. The way I understand the documentation is that multiple
threads can simultaneously be reading an instance of Hashtable while
at the same time a single thread is writing to it. Is this correct?

Yes.

The second part of my question is what exactly is considered a "write"
if I've added a reference object as the value? Is it just calls to
Hashtable methods like .Add and .Remove as well as setting the key to
a new value? Can I safely modify or reassign the reference object in
the Hashtable value? So for instance is this code thread safe?

What's not fine is anything that modifies the hashtable itself. Note
that the hashtable contains references to objects, not objects
themselves - so modifying fields of those objects does not modify the
hashtable (though it does mess things up if the modified fields are
used in GetHashCode() for the object - but this is separate, and
regardless of any threading issues).

class Server
{
    public IPEndPoint serverEndPoint;

}

Hashtable ht = new Hashtable();
Server server = new Server();

server.serverEndPoint = new IPEndPoint(123456, 8000);

ht.Add(1, server);

server.serverEndPoint.Port = 8001;

Maybe ok, maybe not - depends on the implementation of
IPEndPoint.GetHashCode(). Regardless, no threading issues here.

What about?

class Server
{
    public IPEndPoint serverEndPoint;

}

Hashtable ht = new Hashtable();
Server server = new Server();

server.serverEndPoint = new IPEndPoint(123456, 8000);

ht.Add(1, server);

server = new Server();
server.serverEndPoint = new IPEndPoint(123456, 8001);

This is definitely fine. You add the value of variable "server" -
which was a reference to some object - to the hashtable. The variable
itself is not linked to hashtable in any way, so when you later change
it (to reference a new object), this has no effect on the hashtable.

By the way, are you only using Hashtable, and not
Dictionary<TKey,TValue>, because you want to get locking for free?
.



Relevant Pages

  • Re: message communication (VMS <> PC)
    ... The server was on a PC; I needed code to allow a VMS client (one per ... reference local_addr. ... if sys_status not = ss$_normal go to fini. ...
    (comp.os.vms)
  • Re: Thread safety help on Hashtable
    ... locking on the reads which as I understand a Dictionary ... if I've added a reference object as the value? ...     public IPEndPoint serverEndPoint; ... Server server = new Server; ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: 4-way Opteron vs. Xeon-IBM X3 architecture
    ... >>>The point here is that the issue concerns both speed AND capacity. ... >As to the reference, the message header points right back in this thread, ... >>>As you may suspect, I read plenty about memory systems, and I would ... >>>from the enthusiast market and assumed that it would work in the server ...
    (comp.sys.ibm.pc.hardware.chips)
  • Re: Web part development environment
    ... Just to verify: is the config you describe how you are set up? ... To create a Web Part, you need to add a reference to the ... you will need access to a server running Windows SharePoint Services. ... > from Microsoft and installed on the Dev machine. ...
    (microsoft.public.sharepoint.portalserver.development)
  • Re: Isolated Network Drift Problem
    ... they need some type of time reference if the master server becomes ... without a local clock reference will be able to provide time to NTP ... Isn't having an "apparently" synchronized NTP server better than ...
    (comp.protocols.time.ntp)