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

Interlocked on a string?

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

Feb 27 '07 #1
Share this Question
Share on Google+
4 Replies


P: n/a
rosco <kr**********@Avalon-automation.bewrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Feb 27 '07 #2

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

--
Göran Andersson
_____
http://www.guffa.com
Feb 27 '07 #3

P: n/a
Göran Andersson <gu***@guffa.comwrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Feb 27 '07 #4

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

--
William Stacey [C# MVP]
PCR concurrency library: www.codeplex.com/pcr
PSH Scripts Project www.codeplex.com/psobject

Feb 28 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.