Interlocked on a string?

R

rosco

I have a multi threading application where multiple threads can read
and write to the property.
I try to avoid the lock statement on properties that hold none
business critical data.
I could create a special '_propertyLock' object for this none critical
data but this could lead to more multithreading problems like
deadlocks.
I am just looking for atomic read a write operation to a string. There
is a Read member but only for typeof long.

Can I use Interlocked on a string?

private string _name
public string Name
{
get { return _name; }
set { System.Threading.Interlocked.Exchange(ref _name,
value);
}


I TRY TO AVOID ...
private object _propertyLock1 = new object()
private string _name1
public string Name1
{
get { lock(propertyLock1 ){return _name1; }}
set { lock(propertyLock1 ){_name1 = value;}}
}

private object _propertyLock2 = new object()
private string _name2
public string Name2
{
get { lock(propertyLock 2){return _name2; }}
set { lock(propertyLock 2){_name = value2;}}
}
 
J

Jon Skeet [C# MVP]

rosco said:
I have a multi threading application where multiple threads can read
and write to the property.
I try to avoid the lock statement on properties that hold none
business critical data.
I could create a special '_propertyLock' object for this none critical
data but this could lead to more multithreading problems like
deadlocks.

Not if you're careful. If a lock is only used for simple properties,
it's very easy to make sure that doesn't contribute to deadlocks.
I am just looking for atomic read a write operation to a string. There
is a Read member but only for typeof long.

Can I use Interlocked on a string?

No. You can make it volatile, which is probably good enough - but
personally I'd use a lock.
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

rosco said:
I have a multi threading application where multiple threads can read
and write to the property.
I try to avoid the lock statement on properties that hold none
business critical data.
I could create a special '_propertyLock' object for this none critical
data but this could lead to more multithreading problems like
deadlocks.
I am just looking for atomic read a write operation to a string. There
is a Read member but only for typeof long.

Can I use Interlocked on a string?

private string _name
public string Name
{
get { return _name; }
set { System.Threading.Interlocked.Exchange(ref _name,
value);
}


I TRY TO AVOID ...
private object _propertyLock1 = new object()
private string _name1
public string Name1
{
get { lock(propertyLock1 ){return _name1; }}
set { lock(propertyLock1 ){_name1 = value;}}
}

private object _propertyLock2 = new object()
private string _name2
public string Name2
{
get { lock(propertyLock 2){return _name2; }}
set { lock(propertyLock 2){_name = value2;}}
}

A string is a reference type, so what you read and write is a reference.
Assigning a reference _is_ atomic.

There is no Read method for a reference in the Interlocked class because
it's not needed.
 
J

Jon Skeet [C# MVP]

Göran Andersson said:
A string is a reference type, so what you read and write is a reference.
Assigning a reference _is_ atomic.

There is no Read method for a reference in the Interlocked class because
it's not needed.

Assigning a reference is atomic, but not necessarily volatile. If the
OP wants to guarantee that when he reads, he sees a recent value of the
variable rather than one cached from "whenever", using either a lock or
a volatile variable is the way to go.
 
W

William Stacey [C# MVP]

| Can I use Interlocked on a string?
|
| private string _name
| public string Name
| {
| get { return _name; }
| set { System.Threading.Interlocked.Exchange(ref _name,
| value);
| }

You can like below with the type overload of Exchange<T> which takes
reference types.
Interlocked.Exchange<string>(ref this.mystring, "hello");

If it is right for your app would depend on other factors. If you have
other invariants in your class that need to be set atomic in respect to each
other, then a lock may be better.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top