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" <no****@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: <uU**************@tk2msftngp13.phx.gbl>
Newsgroups: microsoft.public.dotnet.framework.aspnet
NNTP-Posting-Host: 12.104.97.34
Path:
cpmsftngxa07.phx.gbl!cpmsftngxa06.phx.gbl!TK2MSFTN GXS01.phx.gbl!TK2MSFTNGXA0
5.phx.gbl!TK2MSFTNGP08.phx.gbl!tk2msftngp13.phx.gb lXref: 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
withvariable 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.