Re: Interlocked.Increment ?

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: George Ter-Saakov (nospam_at_hotmail.com)
Date: 02/03/04


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.
> >
> >
> >
>



Relevant Pages

  • Re: Is the following code MT-Safe?
    ... the assert (which is a fundamental design error: ... and almost always leads to either major synchronization failures ... >Does _bRunning need to be tagged as volatile? ... compiler to cache values, but only during the execution of a function; ...
    (microsoft.public.vc.mfc)
  • Re: Volatile + multithreading
    ... The volatile keyword has little use in multithreaded programming. ... > field or global seems to have no impact because it seems the compiler will ... operations as "special" and suppress optimizations around them. ... > majority of the multithreading issues that could have resulted from ...
    (microsoft.public.vc.language)
  • Re: [PATCH 0/24] make atomic_read() behave consistently across all architectures
    ... If I could trust atomic_read/atomic_setto cause the compiler ... as you've explained it below) -- neither w.r.t. CPU re-ordering (which ... You are correct about CPU re-ordering (and about the fact that this ... The compiler is prohibited from moving a volatile access across a sequence ...
    (Linux-Kernel)
  • Re: Share .cpp and .h along projects
    ... pvector = InterlockedExchangePointerAcquire(&g_sharedVector, ... I really would like to hear what you think volatile accomplishes ... You can't require people to use volatile on top of synchronization. ... compiler useful for multithreaded programming. ...
    (microsoft.public.vc.language)
  • Re: Is the following code MT-Safe?
    ... >>Explanation follows code example, MFC synchronization objects used ... > the first thread to resume, which then takes the assert. ... > volatile is unrelated to synchronization. ... > compiler to cache values, but only during the execution of a function; ...
    (microsoft.public.vc.mfc)