Re: How to use Volatile on field of type long ?

Tech-Archive recommends: Speed Up your PC by fixing your registry



Truong Hong Thi wrote:
> Generally, I won't put lock statement inside get and set accessors like
> above because such things cannot end up in a race condition. Note that
> access is not guaranteed to be synchronized, but it is often
> toleratable.

I'm not sure what you mean by "because such things cannot end up in a
race condition" (or rather, why that would mean you *won't* put locking
in).

My own guidance is really:

1) Only make things thread-safe if they really need to be
2) Document the thread-safety thoroughly
3) If you need to be thread-safe, don't try to be clever until you know
you've got a performance problem. Using locks is a very simple way of
getting things right (if you're reasonably careful). Things like
double-checked locking are just asking for trouble.
4) Unless you need to expose something for other objects to lock on,
always lock on private references. If you need multiple locks, document
carefully what order they should be taken out in.

Jon

.



Relevant Pages

  • Writing a thread-safe class
    ... For this I have a lock attribute in my class obtained with threading.Lock, in the constructor, and every method begins by acquiring the lock and ends by releasing it ... My problem is that the lock is still an attribute of the class, and the very expression self.lock in the statement self.lock.acquireis performed before locking, thus not being thread-safe. ... If I am correct self is a dictionary of object attributes, and if another thread has the lock and creates a new attribute, or deletes one, in the same time with my lookup for self.lock, than the lookup is compromised. ... How do people create thread-safe classes in python? ...
    (comp.lang.python)
  • Re: multi threading in C#
    ... the queue itself does. ... Let's assume the Queue class is defined as not thread-safe. ... But then, if the class is defined as not thread-safe, why would it be doing any locking itself? ... Interestingly at least part of the issue comes about because of the use of the Monitor class, which apparently has knowledge of the compiler keyword "lock". ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: multi threading in C#
    ... the queue itself does. ... Let's assume the Queue class is defined as not thread-safe. ... It may be doing locking in a different way, ... It may have some internal data structure that it needs a lock ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Using shell commands versus C equivalents
    ... Should I briefly lock (flock) the file when running open/fstat/fchmod then ... This operation is not something that needs to be thread-safe. ... the same port multiple times at the exact same time, ...
    (freebsd-hackers)
  • Re: What is difference between lock(private instance) and lock(private static instance)?
    ... The code is probably not thread-safe. ... MethodToAccessStaticResource and all will acquire the lock (since it ... Now, if sharedResource itself is ... But, yes, in general your understanding is correct. ...
    (microsoft.public.dotnet.languages.csharp)