Re: How to use Volatile on field of type long ?
- From: "Jon Skeet [C# MVP]" <skeet@xxxxxxxxx>
- Date: 29 Nov 2005 05:53:58 -0800
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
.
- Follow-Ups:
- Re: How to use Volatile on field of type long ?
- From: Truong Hong Thi
- Re: How to use Volatile on field of type long ?
- References:
- Re: How to use Volatile on field of type long ?
- From: Truong Hong Thi
- Re: How to use Volatile on field of type long ?
- From: Truong Hong Thi
- Re: How to use Volatile on field of type long ?
- Prev by Date: Re: How to use Volatile on field of type long ?
- Next by Date: ASP.NET
- Previous by thread: Re: How to use Volatile on field of type long ?
- Next by thread: Re: How to use Volatile on field of type long ?
- Index(es):
Relevant Pages
|