Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / July 2007

Tip: Looking for answers? Try searching our database.

does reading op need semaphore in multi-thread coding?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
fred - 26 Jul 2007 21:28 GMT
hi All,
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
share memory data. I remember it should be fine to read without
protection.
Thank in advance.
Fred
Jon Skeet [C# MVP] - 26 Jul 2007 22:14 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
> share memory data. I remember it should be fine to read without
> protection.

No, it isn't. Depending on exactly what you're doing, the JIT or CPU
may decide to cache the results of an earlier read, not refreshing it
each iteration.

Either store your data in volatile variables, or only touch it inside a
lock, using the same lock to protect both the read and the write of the
data.

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 - 26 Jul 2007 22:22 GMT
> hi All,
> I am working in a multi thread project. is it necessary to use
[quoted text clipped - 3 lines]
> Thank in advance.
> Fred

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.

If you are looping a list or collection that may change in size, though,
that needs to be synchronised.

Signature

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

Jon Skeet [C# MVP] - 26 Jul 2007 23:10 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 - 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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.