473,227 Members | 1,576 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,227 software developers and data experts.

How to use Volatile on field of type long ?

Hi,

i read in documentation that the c# Volatile keyword cann't be use on long
type fields. this field is use by multiple threads but i don't want the
performance overhead of lock(obj) .

What to do ?

Thanks.

Nov 29 '05 #1
10 7597
yaron wrote:
i read in documentation that the c# Volatile keyword cann't be use on long
type fields. this field is use by multiple threads but i don't want the
performance overhead of lock(obj) .

What to do ?


Have you actually measured the performance overhead of locking? In my
experience, very few applications will actually notice any significant
difference if the lock is usually uncontested.

I don't believe there *are* any alternatives in this case.

Jon

Nov 29 '05 #2
Hi,

On 32bit systems, an operation on a long (64 bit) is not guaranteed
atomic. Interlock class has overloads of Increment/Decrement that take
long param are only atomic with respect to each other (that is, as long
as Increment and Decrement are the 2 only operations you ever want to
perform on them); otherwise, you need a lock.
Solution:
- consider using int instead of long if posible
- synchronize access to the var - not much overhead if the contention
is not high

Thi - http://thith.blogspot.com

Nov 29 '05 #3
Hi All,

thanks for your quick resposne.
please let me ask you another question :

Class Player
{
private bool enable;
public bool Enable
{
get {retturn enable;}
set {enable=value;}
}
}

in a multi-threading scenario on Player instance , do i need to put a lock
statement in the Enable property or can i be relax and trust that this is an
atomic operation.
what is good to do ?

Thanks you all.
"Truong Hong Thi" wrote:
Hi,

On 32bit systems, an operation on a long (64 bit) is not guaranteed
atomic. Interlock class has overloads of Increment/Decrement that take
long param are only atomic with respect to each other (that is, as long
as Increment and Decrement are the 2 only operations you ever want to
perform on them); otherwise, you need a lock.
Solution:
- consider using int instead of long if posible
- synchronize access to the var - not much overhead if the contention
is not high

Thi - http://thith.blogspot.com

Nov 29 '05 #4
Generally, I won't put lock statement inside get and set accessors like
above because such things cannot end up in a race condition. Note that
access is not guaranteed to be synchronized, but it is often
toleratable.

Nov 29 '05 #5
is it because the get and set operations are atomic operation in this case ?
let say instead of bool property it was of type long , then would you put a
lock in the get/set methods ?

Thanks a lot.
"Truong Hong Thi" wrote:
Generally, I won't put lock statement inside get and set accessors like
above because such things cannot end up in a race condition. Note that
access is not guaranteed to be synchronized, but it is often
toleratable.

Nov 29 '05 #6
yaron wrote:
is it because the get and set operations are atomic operation in this case ?
let say instead of bool property it was of type long , then would you put a
lock in the get/set methods ?


Atomicity is rarely sufficient, frankly. Unless you don't care whether
or not one thread sees the changes another thread makes, you need to
either use locking or volatility.

See http://www.pobox.com/~skeet/csharp/t...latility.shtml

Jon

Nov 29 '05 #7
Truong Hong Thi wrote:
Generally, I won't put lock statement inside get and set accessors like
above because such things cannot end up in a race condition. Note that
access is not guaranteed to be synchronized, but it is often
toleratable.


I'm not sure what you mean by "because such things cannot end up in a
race condition" (or rather, why that would mean you *won't* put locking
in).

My own guidance is really:

1) Only make things thread-safe if they really need to be
2) Document the thread-safety thoroughly
3) If you need to be thread-safe, don't try to be clever until you know
you've got a performance problem. Using locks is a very simple way of
getting things right (if you're reasonably careful). Things like
double-checked locking are just asking for trouble.
4) Unless you need to expose something for other objects to lock on,
always lock on private references. If you need multiple locks, document
carefully what order they should be taken out in.

Jon

Nov 29 '05 #8
Thanks a lot Jon.

"Jon Skeet [C# MVP]" wrote:
Truong Hong Thi wrote:
Generally, I won't put lock statement inside get and set accessors like
above because such things cannot end up in a race condition. Note that
access is not guaranteed to be synchronized, but it is often
toleratable.


I'm not sure what you mean by "because such things cannot end up in a
race condition" (or rather, why that would mean you *won't* put locking
in).

My own guidance is really:

1) Only make things thread-safe if they really need to be
2) Document the thread-safety thoroughly
3) If you need to be thread-safe, don't try to be clever until you know
you've got a performance problem. Using locks is a very simple way of
getting things right (if you're reasonably careful). Things like
double-checked locking are just asking for trouble.
4) Unless you need to expose something for other objects to lock on,
always lock on private references. If you need multiple locks, document
carefully what order they should be taken out in.

Jon

Nov 29 '05 #9
>I'm not sure what you mean by "because such things cannot end up in a
race condition" I mean such operations are atomic. The bool cannot be partially
updated.
(or rather, why that would mean you *won't* put lockingin).

I did said access to the var is not synchronized. On multi-processor
box, one thread might not see changes made by other thread. In some
situations, that is not a problem. In situations that matters, I often
lock the call to property instead of lock inside the property. That is
whay I said "I won't".

Thi

Nov 30 '05 #10
Hi all,

let be focus on sigle processor machine 32 bits.
1. so if i understand you , then on single processor machine 32bit i don't
have to sync access to atomic get/set properties of type less then 32 bits or
do i still have to use volatile on single processor machine ?

2. does the problems you mention before optimizations/reordering/caching
makes problems with multi-threading on single processor machine 32bit ?

Thanks all.
"Truong Hong Thi" wrote:
I'm not sure what you mean by "because such things cannot end up in a
race condition"

I mean such operations are atomic. The bool cannot be partially
updated.
(or rather, why that would mean you *won't* put locking
in).

I did said access to the var is not synchronized. On multi-processor
box, one thread might not see changes made by other thread. In some
situations, that is not a problem. In situations that matters, I often
lock the call to property instead of lock inside the property. That is
whay I said "I won't".

Thi

Dec 1 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Andrew | last post by:
Section 17.4.3 of the ECMA-334 C# Language Specification says 1 When a field-declaration includes a volatile modifier, the fields introduced by that declaration are volatile fields. 2 For...
9
by: Tim Rentsch | last post by:
I have a question about what ANSI C allows/requires in a particular context related to 'volatile'. Consider the following: volatile int x; int x_remainder_arg( int y ){ return x % y; }
22
by: Assaf | last post by:
hi all i know that i should not cross-post, but i am not sure to which group to post this question. 2 quesions about volatile: 1. i use volatile when 2 threads access the same variable...
13
by: yaron | last post by:
Hi all, let be focus on sigle processor machine 32 bits. 1. with multi-threaded on single processor machine 32bit do i have to sync access to atomic get/set properties of type less then 32 bits...
10
by: Lau Lei Cheong | last post by:
Hello, I really need to use volatile System.Int64 for a .NET v1.1 program in C#. But the compiler complains "a volatile field can not be of type long". How to work around it? Or is there any...
18
by: Mark | last post by:
Hi List, I want to write a function to copy some data out of a hardware buffer. The hardware can change the contents of this buffer without it being written to by my function. I want to use...
6
by: red floyd | last post by:
I have a struct that maps onto a set of memory mapped registers. I access this via a pointer. Is it better to declare it as pointer to a volatile struct, or to declare the individual members as...
6
by: titan nyquist | last post by:
Can you make volatile structures in C#? I have a static class, to have "global" variables. This allows the whole program to see them. I make them "volatile" to avoid multi- threading accessing...
94
by: Samuel R. Neff | last post by:
When is it appropriate to use "volatile" keyword? The docs simply state: " The volatile modifier is usually used for a field that is accessed by multiple threads without using the lock...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...

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.