When is "volatile" used instead of "lock" ?
flag
Messages 1 - 10 of 130 - Collapse all
/groups/adfetch?adid=C1RsshEAAADYovHcvlvOwDH3t0bU1hmLFSRgCP-avRN4YT0eROC0jw
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
1.  Samuel R. Neff  
View profile  
 More options May 21 2007, 10:35 am
Newsgroups: microsoft.public.dotnet.languages.csharp
From: Samuel R. Neff <samueln...@nomail.com>
Date: Mon, 21 May 2007 10:35:06 -0400
Local: Mon, May 21 2007 10:35 am
Subject: When is "volatile" used instead of "lock" ?

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 Statement (C# Reference)
statement to serialize access.
"

But when is it better to use "volatile" instead of "lock" ?  

Thanks,

Sam

------------------------------------------------------------
We're hiring!  B-Line Medical is seeking .NET
Developers for exciting positions in medical product
development in MD/DC.  Work with a variety of technologies
in a relaxed team environment.  See ads on Dice.com.


    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
2.  ben.biddington@gmail.com  
View profile  
 More options May 21 2007, 11:28 am
Newsgroups: microsoft.public.dotnet.languages.csharp
From: "ben.bidding...@gmail.com" <ben.bidding...@gmail.com>
Date: 21 May 2007 08:28:20 -0700
Local: Mon, May 21 2007 11:28 am
Subject: Re: When is "volatile" used instead of "lock" ?
On May 21, 3:35 pm, Samuel R. Neff <samueln...@nomail.com> wrote:

You can also the System.Threading.Interlocked class which maintains
volatile semantics.

Seealso: http://www.albahari.com/threading/part4.html


    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
3.  Jon Skeet [ C# MVP ]  
View profile  
 More options May 21 2007, 1:05 pm
Newsgroups: microsoft.public.dotnet.languages.csharp
From: Jon Skeet [C# MVP] <sk...@pobox.com>
Date: Mon, 21 May 2007 18:05:34 +0100
Local: Mon, May 21 2007 1:05 pm
Subject: Re: When is "volatile" used instead of "lock" ?

ben.bidding...@gmail.com <ben.bidding...@gmail.com> wrote:
> You can also the System.Threading.Interlocked class which maintains
> volatile semantics.

> Seealso: http://www.albahari.com/threading/part4.html

But only if you use it for both the writing *and* the reading, which
isn't terribly obvious from the docs.

--
Jon Skeet - <sk...@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


    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
4.  Ben Voigt  
View profile  
 More options May 22 2007, 6:48 pm
Newsgroups: microsoft.public.dotnet.languages.csharp
From: "Ben Voigt" <r...@nospam.nospam>
Date: Tue, 22 May 2007 17:48:56 -0500
Local: Tues, May 22 2007 6:48 pm
Subject: Re: When is "volatile" used instead of "lock" ?

<ben.bidding...@gmail.com> wrote in message

news:1179761300.206885.51230@z28g2000prd.googlegroups.com...

You should use volatile and Interlocked together, neither fully replaces the
other.


    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
5.  Willy Denoyette [MVP]  
View profile  
 More options May 23 2007, 3:43 am
Newsgroups: microsoft.public.dotnet.languages.csharp
From: "Willy Denoyette [MVP]" <willy.denoye...@telenet.be>
Date: Wed, 23 May 2007 09:43:04 +0200
Local: Wed, May 23 2007 3:43 am
Subject: Re: When is "volatile" used instead of "lock" ?
"Ben Voigt" <r...@nospam.nospam> wrote in message

news:uln5JOMnHHA.3968@TK2MSFTNGP06.phx.gbl...

Not necessarily, there is no need for volatile, as long you Interlock
consistently across all threads in the process. This means that once you
access a shared variable using Interlock, all threads should use Interlock.

Willy.


    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
6.  Ben Voigt  
View profile  
 More options May 23 2007, 9:08 am
Newsgroups: microsoft.public.dotnet.languages.csharp
From: "Ben Voigt" <r...@nospam.nospam>
Date: Wed, 23 May 2007 08:08:40 -0500
Local: Wed, May 23 2007 9:08 am
Subject: Re: When is "volatile" used instead of "lock" ?

"Willy Denoyette [MVP]" <willy.denoye...@telenet.be> wrote in message
news:1B1A6E75-67F7-4B6E-B858-C7517C5BEA43@microsoft.com...

I don't think so, actually.  Without volatile semantics, the compiler is
free to cache the value of any parameter, including in/out parameters.  Say
you are calling an Interlocked method in a loop.  If the variable is not
volatile, the compiler can actually call Interlocked on a local copy, and
then write the value to the real variable once, at the end of the loop (and
worse, it can do so in a non-atomic way).  Anything that maintains correct
operation from the perspective of the calling thread is permissible for
non-volatile variable access.  Why would a compiler do this?  For optimal
use of cache.  By using a local copy of a variable passed byref, locality of
reference is improved, and additionally, a thread's stack (almost) never
incurs cache coherency costs.

Note that this is not a problem for pass-by-pointer, which must use the true
address of the referenced variable in order to enable pointer arithmetic.
But pointer arithmetic isn't allowed for tracking handles, a handle is an
opaque value anyway.

For lockless data structures, always use volatile.  And then stick that
volatile variable close in memory to what it is protecting, because CPU
cache has to load and flush an entire cache line at once, and volatile write
semantics require flushing all pending writes.


    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
7.  Brian Gideon  
View profile  
 More options May 23 2007, 12:22 pm
Newsgroups: microsoft.public.dotnet.languages.csharp
From: Brian Gideon <briangid...@yahoo.com>
Date: 23 May 2007 09:22:47 -0700
Local: Wed, May 23 2007 12:22 pm
Subject: Re: When is "volatile" used instead of "lock" ?
On May 23, 8:08 am, "Ben Voigt" <r...@nospam.nospam> wrote:

The Interlocked methods have volatile semantics.  So as long as you
consistently use them for both reading and writing the end result
should be the same.

    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
8.  Ben Voigt  
View profile  
 More options May 24 2007, 10:28 am
Newsgroups: microsoft.public.dotnet.languages.csharp
From: "Ben Voigt" <r...@nospam.nospam>
Date: Thu, 24 May 2007 09:28:16 -0500
Local: Thurs, May 24 2007 10:28 am
Subject: Re: When is "volatile" used instead of "lock" ?

"Brian Gideon" <briangid...@yahoo.com> wrote in message

news:1179937367.347627.145620@q75g2000hsh.googlegroups.com...

Inside the implementation.  But who guarantees that the variable atomically
read and written to, is your so-called "end result"?  No one, unless you use
volatile, forcing the compiler to generate a reference to the actual
variable every time you mention it.

    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
9.  Jon Skeet [C# MVP]  
View profile  
 More options May 24 2007, 10:47 am
Newsgroups: microsoft.public.dotnet.languages.csharp
From: "Jon Skeet [C# MVP]" <sk...@pobox.com>
Date: 24 May 2007 07:47:40 -0700
Local: Thurs, May 24 2007 10:47 am
Subject: Re: When is "volatile" used instead of "lock" ?
On May 24, 3:28 pm, "Ben Voigt" <r...@nospam.nospam> wrote:

> > The Interlocked methods have volatile semantics.  So as long as you
> > consistently use them for both reading and writing the end result
> > should be the same.

> Inside the implementation.

No, inside the JIT which has to notice that you call Interlocked.

> But who guarantees that the variable atomically
> read and written to, is your so-called "end result"?

The CLI spec.

By the way, if you pass volatile parameters by reference, the
volatility is irrelevant as far as the called method is concerned
anyway - it even triggers a warning from the compiler (CS0420).

Jon


    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
10.  Ben Voigt  
View profile  
 More options May 24 2007, 10:52 am
Newsgroups: microsoft.public.dotnet.languages.csharp
From: "Ben Voigt" <r...@nospam.nospam>
Date: Thu, 24 May 2007 09:52:04 -0500
Local: Thurs, May 24 2007 10:52 am
Subject: Re: When is "volatile" used instead of "lock" ?

"Jon Skeet [C# MVP]" <sk...@pobox.com> wrote in message
news:1180018060.019847.127350@p77g2000hsh.googlegroups.com...

> On May 24, 3:28 pm, "Ben Voigt" <r...@nospam.nospam> wrote:
>> > The Interlocked methods have volatile semantics.  So as long as you
>> > consistently use them for both reading and writing the end result
>> > should be the same.

>> Inside the implementation.

> No, inside the JIT which has to notice that you call Interlocked.

How can it, with dynamically generated code and reflection?

>> But who guarantees that the variable atomically
>> read and written to, is your so-called "end result"?

> The CLI spec.

> By the way, if you pass volatile parameters by reference, the
> volatility is irrelevant as far as the called method is concerned
> anyway - it even triggers a warning from the compiler (CS0420).

C++ has the concept of pointer (or reference) to volatile, letting a
function declare to its callers that a parameter will be treated as
volatile.  Doesn't .NET have the same?  It seems like the only way to
correctly resolve CS0420.

Well, .NET Reflector doesn't show any type of annotation on Interlocked.Add,
for example, so I guess it does not.


    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2010 Google