By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,657 Members | 774 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,657 IT Pros & Developers. It's quick & easy.

Interlocked.Increment ?

P: n/a
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.
Nov 18 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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.


Nov 18 '05 #2

P: n/a
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]" <fe*****@online.microsoft.com> wrote in message
news:nK**************@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" <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 l
Xref: cpmsftngxa07.phx.gbl microsoft.public.dotnet.framework.aspnet:206956X-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 cannot think of any use of it.
Thanks.
George.

Nov 18 '05 #3

P: n/a
Hi George,

You are right. We use volatile modifier to tell the optimizer that the
variable should not be moved to a register rather it should be accessed
from memory all the time.This can avoid reading a cached value. In your
case, you can use Interlock.* methods
(Increment/Decrement/Exchange/CompareExchange) to read and write the
variable instead of using volatiles. If I remember correctly, access with
Interlocked.* method is natively volatile.

Regards,

Felix Wu
=============
This posting is provided "AS IS" with no warranties, and confers no rights.
--------------------
From: "George Ter-Saakov" <no****@hotmail.com>
References: <uU**************@tk2msftngp13.phx.gbl> <nK**************@cpmsftngxa07.phx.gbl>Subject: Re: Interlocked.Increment ?
Date: Tue, 3 Feb 2004 07:52:09 -0500
Lines: 96
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: <eQ**************@TK2MSFTNGP10.phx.gbl>
Newsgroups: microsoft.public.dotnet.framework.aspnet
NNTP-Posting-Host: 12.104.97.34
Path: cpmsftngxa07.phx.gbl!cpmsftngxa06.phx.gbl!TK2MSFTN GP08.phx.gbl!TK2MSFTNGP10.
phx.gblXref: cpmsftngxa07.phx.gbl microsoft.public.dotnet.framework.aspnet:207140
X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet

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]" <fe*****@online.microsoft.com> wrote in message
news:nK**************@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" <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!TK2MSFT NGXS01.phx.gbl!TK2MSFTNGXA

0
5.phx.gbl!TK2MSFTNGP08.phx.gbl!tk2msftngp13.phx.gb l
>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 ican >not think of any use of it.
>
>
>Thanks.
>George.
>
>
>



Nov 18 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.