Re: Interlocked.Increment ?
From: George Ter-Saakov (nospam_at_hotmail.com)
Date: 02/03/04
- Next message: Tommy: "Re: Globalising application?"
- Previous message: Ashley: "Internet explorer"
- In reply to: Felix Wu [MSFT]: "RE: Interlocked.Increment ?"
- Next in thread: Felix Wu [MSFT]: "Re: Interlocked.Increment ?"
- Reply: Felix Wu [MSFT]: "Re: Interlocked.Increment ?"
- Messages sorted by: [ date ] [ thread ]
Date: Tue, 3 Feb 2004 07:52:09 -0500
My understanding is that volatile word needs to be specified so compiler
will know not to optimize access to it.
Basically following code
i = 6;
k = i*8;
will give a result 48 even if i was changed in another thread.
Because compiler will figure out that i is not changed and it can safely
assign k a 48 in runtime.
volatile i = 6;
k = i*8;
Will not be optimized since compiler will know that i could have been
changed.
And in my opinion the variable used in Interlocked.Increment must be
declared as volatile since it's definitely will be changed in different
threads.
George.
"Felix Wu [MSFT]" <felixwu@online.microsoft.com> wrote in message
news:nK6RrKg6DHA.1992@cpmsftngxa07.phx.gbl...
> Hi George,
>
> InterLocked .Increment is not targeted to be used with volatile modifier.
I
> think if you are using InterLocked class, you don't need to use volatile.
> The volatile modifier is usually used for a field that is accessed by
> multiple threads without using the lock statement to serialize access.
>
> According to C# Language Specification (10.4.3 Volatile fields), for
> non-volatile fields, optimization techniques that reorder instructions can
> lead to unexpected and unpredictable results in multi-threaded programs
> that access fields without synchronization such as that provided by the
> lock-statement. To avoid this problem, we can use volatile modifier to
> guarantee safe access to the field.
>
> You can use InterLocked class to serialize access to the field to make it
> thread safe.
>
> Regards,
>
> Felix Wu
> =============
> This posting is provided "AS IS" with no warranties, and confers no
rights.
>
>
> --------------------
> >From: "George Ter-Saakov" <nospam@hotmail.com>
> >Subject: Interlocked.Increment ?
> >Date: Mon, 2 Feb 2004 13:29:08 -0500
> >Lines: 17
> >X-Priority: 3
> >X-MSMail-Priority: Normal
> >X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
> >X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
> >Message-ID: <uUvDZpb6DHA.2908@tk2msftngp13.phx.gbl>
> >Newsgroups: microsoft.public.dotnet.framework.aspnet
> >NNTP-Posting-Host: 12.104.97.34
> >Path:
>
cpmsftngxa07.phx.gbl!cpmsftngxa06.phx.gbl!TK2MSFTNGXS01.phx.gbl!TK2MSFTNGXA0
> 5.phx.gbl!TK2MSFTNGP08.phx.gbl!tk2msftngp13.phx.gbl
> >Xref: cpmsftngxa07.phx.gbl
microsoft.public.dotnet.framework.aspnet:206956
> >X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
> >
> >What is the purpose of having Interlocked.Increment if it does not work
> with
> >variable declared as volatile.
> >
> >Here is my problem,
> > Interlocked.Increment increments the variable in thread safe manner.
> >But at the same time if i want to use variable that could be changed in
> >another thread i must use volatile (to prevent optimization).
> >
> >But then i can not use Interlocked.Increment.
> >So i do not see any benefits of having Interlocked.Increment because i
can
> >not think of any use of it.
> >
> >
> >Thanks.
> >George.
> >
> >
> >
>
- Next message: Tommy: "Re: Globalising application?"
- Previous message: Ashley: "Internet explorer"
- In reply to: Felix Wu [MSFT]: "RE: Interlocked.Increment ?"
- Next in thread: Felix Wu [MSFT]: "Re: Interlocked.Increment ?"
- Reply: Felix Wu [MSFT]: "Re: Interlocked.Increment ?"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|