
Signature
Göran Andersson
_____
http://www.guffa.com
> > I am working in a multi thread project. is it necessary to use
> > semaphore when doing reading operation. in my case I do loop to read
[quoted text clipped - 4 lines]
> Each single read is atomic, and is guaranteed not to give you a halfway
> changed value, as long as it's a native data type.
No - unless you deem "double" and "long" not to be native data types.
> If you are looping a list or collection that may change in size, though,
> that needs to be synchronised.
There's more to worry about than atomicity - there's volatility too.
Suppose the value of an int variable changes from 0 to 5. Without any
extra work, I know that I'll never see any value *other* than 0 or 5
(due to atomicity) but there's no guarantee that I'll *ever* see 5.
That's not good for most scenarios :)

Signature
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Göran Andersson - 27 Jul 2007 01:19 GMT
>>> I am working in a multi thread project. is it necessary to use
>>> semaphore when doing reading operation. in my case I do loop to read
[quoted text clipped - 5 lines]
>
> No - unless you deem "double" and "long" not to be native data types.
Right. "Native" wasn't exactly what I was after.
>> If you are looping a list or collection that may change in size, though,
>> that needs to be synchronised.
[quoted text clipped - 4 lines]
> (due to atomicity) but there's no guarantee that I'll *ever* see 5.
> That's not good for most scenarios :)
Good point.

Signature
Göran Andersson
_____
http://www.guffa.com