473,396 Members | 2,024 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

volatile fields and lock

Hello.
How come it's safe to read non-volatile fields that are shared across
threads using locks for synchronization (for example, Monitor.Enter or
EventWaitHandle), but it's not safe to access them if no lock synchronization
mechanisms are employed?
Whe using locks, I'm I guaranteed that when accessing shared non-volatile
fields I will get the most up-to-date values?
Thanks.
Sep 18 '06 #1
3 1762
Amir,

The lock will ensure two things. First, it prevents other threads from
mucking with the shared data which would cause race conditions leading
to potentially half-read or half-written variables. Second, it creates
a memory barrier to ensure that both reads and writes are visible to
all threads and that the ordering of those reads and writes cannot move
beyond the bounds of the lock.

Yes, you are guarenteed to get the most recent value when using the
lock keyword.

For a more detailed explanation read the following article.

http://www.yoda.arachsys.com/csharp/...latility.shtml

Brian

Amir Shitrit wrote:
Hello.
How come it's safe to read non-volatile fields that are shared across
threads using locks for synchronization (for example, Monitor.Enter or
EventWaitHandle), but it's not safe to access them if no lock synchronization
mechanisms are employed?
Whe using locks, I'm I guaranteed that when accessing shared non-volatile
fields I will get the most up-to-date values?
Thanks.
Sep 18 '06 #2
"Amir Shitrit" <Am*********@discussions.microsoft.comwrote
How come it's safe to read non-volatile fields that are shared across
threads using locks for synchronization (for example, Monitor.Enter or
EventWaitHandle), but it's not safe to access them if no lock
synchronization mechanisms are employed?
That's a simple question that has a very deep answer. :(

In short, using a threading construct luch as a Monitor places what's called
a Memory Barrier into the code and let's the compiler know that it can't
cache things locally. You can (in .Net 2.0) play some fun Memory Barrier
games, but it's not really worth the trouble.
Whe using locks, I'm I guaranteed that when accessing shared non-volatile
fields I will get the most up-to-date values?
Yup.

Now, if you really want to have fun, go ahead and ask:

- Should you perform Interlocked operations on Volatile variables.
- Why is there no such thing as a 64-bit volatile field?
- If you can access Variable with Interlocked operations, what's the point
of Volatile?
- If you can have Volatile variables, what's the point of Interlocked
Operations?
- Are volatiles thread safe, or do you still need to use locks?
- Why does the Thread Class have VolatileRead and VolatileWrite - Wouldn't
this create oppertunities for weird stale-data issues if it's not accesses
this way in all cases?

--
Chris Mullins, MCSD.NET, MCPD:Enterprise
http://www.coversant.net/blogs/cmullins
Sep 18 '06 #3
OK.
Do you have answers to these questions as well?

"Chris Mullins" wrote:
"Amir Shitrit" <Am*********@discussions.microsoft.comwrote
How come it's safe to read non-volatile fields that are shared across
threads using locks for synchronization (for example, Monitor.Enter or
EventWaitHandle), but it's not safe to access them if no lock
synchronization mechanisms are employed?

That's a simple question that has a very deep answer. :(

In short, using a threading construct luch as a Monitor places what's called
a Memory Barrier into the code and let's the compiler know that it can't
cache things locally. You can (in .Net 2.0) play some fun Memory Barrier
games, but it's not really worth the trouble.
Whe using locks, I'm I guaranteed that when accessing shared non-volatile
fields I will get the most up-to-date values?

Yup.

Now, if you really want to have fun, go ahead and ask:

- Should you perform Interlocked operations on Volatile variables.
- Why is there no such thing as a 64-bit volatile field?
- If you can access Variable with Interlocked operations, what's the point
of Volatile?
- If you can have Volatile variables, what's the point of Interlocked
Operations?
- Are volatiles thread safe, or do you still need to use locks?
- Why does the Thread Class have VolatileRead and VolatileWrite - Wouldn't
this create oppertunities for weird stale-data issues if it's not accesses
this way in all cases?

--
Chris Mullins, MCSD.NET, MCPD:Enterprise
http://www.coversant.net/blogs/cmullins
Sep 19 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Andrew | last post by:
Section 17.4.3 of the ECMA-334 C# Language Specification says 1 When a field-declaration includes a volatile modifier, the fields introduced by that declaration are volatile fields. 2 For...
22
by: Assaf | last post by:
hi all i know that i should not cross-post, but i am not sure to which group to post this question. 2 quesions about volatile: 1. i use volatile when 2 threads access the same variable...
14
by: Pierre | last post by:
Using the "volatile" keyword, creates a problem if I intend to use any of the interlocked APIs. The compiler generates an error if I use the following line, for example: ...
13
by: yaron | last post by:
Hi all, let be focus on sigle processor machine 32 bits. 1. with multi-threaded on single processor machine 32bit do i have to sync access to atomic get/set properties of type less then 32 bits...
94
by: Samuel R. Neff | last post by:
When is it appropriate to use "volatile" keyword? The docs simply state: " The volatile modifier is usually used for a field that is accessed by multiple threads without using the lock...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.