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 " But when is it better to use "volatile" instead of "lock" ? Thanks, Sam ------------------------------------------------------------ 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.
| ||||||||||||||
Newsgroups: microsoft.public.dotnet.languages.csharp
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. 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.
| ||||||||||||||
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: But only if you use it for both the writing *and* the reading, which > You can also the System.Threading.Interlocked class which maintains > volatile semantics. > Seealso: http://www.albahari.com/threading/part4.html isn't terribly obvious from the docs. -- 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.
| ||||||||||||||
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" ?
news:1179761300.206885.51230@z28g2000prd.googlegroups.com...
You should use volatile and Interlocked together, neither fully replaces the other. 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.
| ||||||||||||||
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" ?
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. 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.
| ||||||||||||||
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 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 For lockless data structures, always use volatile. And then stick that 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.
| ||||||||||||||
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. 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.
| ||||||||||||||
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" ?
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. 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.
| ||||||||||||||
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 No, inside the JIT which has to notice that you call Interlocked. > > consistently use them for both reading and writing the end result > > should be the same. > Inside the implementation. > But who guarantees that the variable atomically The CLI spec. > read and written to, is your so-called "end result"? By the way, if you pass volatile parameters by reference, the Jon 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.
| ||||||||||||||
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 > On May 24, 3:28 pm, "Ben Voigt" <r...@nospam.nospam> wrote: How can it, with dynamically generated code and reflection? >> > 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 > The CLI spec. > By the way, if you pass volatile parameters by reference, the 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, 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 |