473,416 Members | 1,503 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,416 software developers and data experts.

Interlocked.Increment ?

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
3 2718
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: Pierre | last post by:
Using the "volatile" keyword, creates a problem if I intend to use any of the interlocked APIs. The compiler generates an error if I use the following line, for example: ...
19
by: steve | last post by:
// What I want to do Use enumerated types with the Interlocked.Exchange methods Suggestions please // My estimation of problem Seems like Interlocked.Exchange only works with ints,...
3
by: Ryan Liu | last post by:
Hi, Can some one tell the criteria I can used to decide use of Interlocked.Increment() vs volatile , which is better? Thanks a lot! Ryan
23
by: Anders Borum | last post by:
Hi! I am implementing a threaded producer / consumer pattern just for fun. I am using an internal counter to keep track of the produced / consumed items and am logging that information. I am...
15
by: Ryan Liu | last post by:
Hi, Is there any known bug related to Interlocked.Increment(ref var)? My client report var's value going up and down in the client/server multile-thread application. There are about 80...
5
by: Mark Salsbery [MVP] | last post by:
I have an member variable (int) that is accessed by multiple threads using Interlocked.Increment(), Interlocked.Decrement(), and read directly. Using volatile gives me "CS0420: a reference to a...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.