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

Using String.Empty as a SyncLock object

Are there any reasons against writing code like this?

class Foo
{
static string _SyncLock = String.Empty;

public void ThreadSafeMethod()
{
lock (_SyncLock)
{
// Code goes here
}
}
}

Mar 7 '07 #1
12 3127
On Mar 7, 7:51 am, iliti...@gmail.com wrote:
Are there any reasons against writing code like this?

class Foo
{
static string _SyncLock = String.Empty;

public void ThreadSafeMethod()
{
lock (_SyncLock)
{
// Code goes here
}
}

}
I don't see anything wrong with it; you're locking on an instance,
what it contains is irrelevant. An object may have less overhead
though, because it doesn't need a character array..

Mar 7 '07 #2
On Mar 7, 7:51 am, iliti...@gmail.com wrote:
Are there any reasons against writing code like this?

class Foo
{
static string _SyncLock = String.Empty;

public void ThreadSafeMethod()
{
lock (_SyncLock)
{
// Code goes here
}
}

}
My only concern would be that string.Empty may be a singleton, and if
other code decides to lock on it, you could head a deadlock
condition. Might be better just to lock on an object.

Mar 7 '07 #3
On Mar 7, 6:51 am, iliti...@gmail.com wrote:
Are there any reasons against writing code like this?

class Foo
{
static string _SyncLock = String.Empty;

public void ThreadSafeMethod()
{
lock (_SyncLock)
{
// Code goes here
}
}

}
Hi,

I'm fairly confident that String.Empty returns the same instance
everytime. That means if you're using this strategy consistently
throughout your application you'll have completely independent
sections of code competing for the same lock. Why not instantiate an
Object for this purpose?

Brian

Mar 7 '07 #4
Thanks guys. I had the feeling this was the case but I just wanted to
make sure. This code is part of a .NET 1.1 application that I'm
supposed to migrate so I guess I might as well start fixing things
like this as well.

Mar 7 '07 #5
il******@gmail.com wrote:
Are there any reasons against writing code like this?

class Foo
{
static string _SyncLock = String.Empty;

public void ThreadSafeMethod()
{
lock (_SyncLock)
{
// Code goes here
}
}
}
As already have been said, String.Empty always is the same instance, so
locking on that means that all locks block each other even if they are
completely independend. Use new Object() to create a unique object for
the lock.

When locking in a static method, use a static variable for locking, but
when locking in an instance method, use an instance variable for locking.

--
Göran Andersson
_____
http://www.guffa.com
Mar 7 '07 #6
"Göran Andersson" <gu***@guffa.comha scritto nel messaggio news:%
As already have been said, String.Empty always is the same instance, so
locking on that means that all locks block each other even if they are
completely independend.
When you do

static string _SyncLock = String.Empty;

You create a new istance, so the problem isn't the string.Empty, but the
"static" of _SyncLock.
Of course, a

object _SyncLock = new object();

would be more clear.

Mar 7 '07 #7
Fabio wrote:
"Göran Andersson" <gu***@guffa.comha scritto nel messaggio news:%
>As already have been said, String.Empty always is the same instance,
so locking on that means that all locks block each other even if
they are completely independend.

When you do

static string _SyncLock = String.Empty;

You create a new istance, so the problem isn't the string.Empty, but
the "static" of _SyncLock.
Why do you say that? "=" doesn't create new instances, it assigns
references.
Mar 7 '07 #8
"Mike Schilling" <ap@newsgroup.nospamha scritto nel messaggio news:%
Why do you say that? "=" doesn't create new instances, it assigns
references.
It's a string: it's immutable.
Mar 7 '07 #9
"Fabio" <zn*******@virgilio.itwrote in message
news:O3**************@TK2MSFTNGP06.phx.gbl...
"Mike Schilling" <ap@newsgroup.nospamha scritto nel messaggio news:%
>Why do you say that? "=" doesn't create new instances, it assigns references.

It's a string: it's immutable.
Sure, but that's not the point, you are setting a reference to an existing instance. More,
as others have said, there is only one instance of String.Empty per process, that means that
each reference to String.Empty will point to the same instance.

Willy.

Mar 7 '07 #10
It's a string: it's immutable.
>
Sure, but that's not the point, you are setting a reference to an existing instance. More,
as others have said, there is only one instance of String.Empty per process, that means that
each reference to String.Empty will point to the same instance.
Exactly. MSDN is actually very clear on this point:

http://msdn2.microsoft.com/en-us/library/c5kehkcz.aspx
"lock("myLock") is a problem since any other code in the process using
the same string, will share the same lock."

Zytan

Mar 7 '07 #11
<il******@gmail.comwrote:
Are there any reasons against writing code like this?
Absolutely - *anything* else that decides to do the same thing will be
using the same lock, even though you really don't want to! It's just
*waiting* for a deadlock...

Why use that rather than just a plain new object?

--
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
Mar 7 '07 #12
Fabio <zn*******@virgilio.itwrote:
"Mike Schilling" <ap@newsgroup.nospamha scritto nel messaggio news:%
Why do you say that? "=" doesn't create new instances, it assigns
references.

It's a string: it's immutable.
That's irrelevant - or in fact, it's the only reason that string.Empty
*can* legitimately return a reference to the same object every time).
Try this:

using System;

class Test
{
static void Main()
{
string x = string.Empty;
string y = string.Empty;

Console.WriteLine (object.ReferenceEquals(x,y));
}
}

--
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
Mar 7 '07 #13

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

Similar topics

3
by: Keith Langer | last post by:
I would like to know why locking on a new object takes significantly longer than locking on a strongly typed object. All of the posts I've seen say that it is best to lock on a "new object", but...
6
by: Paul | last post by:
Hi. Just trying to find out the best approach as I beleive it might give me problems later on down the road. I have an ASP.NET application which references a shared database class which...
3
by: kelkel | last post by:
Hi all, I got a question here which about using queue. i have create a class of queue. Public Sub Add(ByVal data As Object) SyncLock Me _Data.Enqueue(data) Monitor.Pulse(Me) End SyncLock End...
4
by: fred | last post by:
If I have multiple threads running a Sub as below then a number of threads can be held up at the SyncLock. When it becomes free which thread goes first. Is it just by chance which thread goes first...
10
by: Bob Day | last post by:
Using vs 2003, vb.net sql msde.. Consider the following code snippets. See **** for questions. All are shared and accessed by multiple threads simultaneiously. ' Instantiate per for this...
4
by: Lucas Tam | last post by:
Is there a document online which details the advantages of using a Threadpool? My application uses a user configuration amount of threads which does the following: Main Thread Gives Work to...
4
by: Jeff Stewart | last post by:
Specifically, I don't understand the parameter that Synclock accepts. How is a reference type a lockable entity? What -is- a reference type? Is it a number? Is it a value at a specific memory...
2
by: Ramta | last post by:
Hi all, I am trying to develop a Producer thread that listens for UDP packets on a socket and store then in an ArrayList. The consumer should read data from list and block when no element is...
2
by: HONOREDANCESTOR | last post by:
I have a buffer that needs to be locked sometimes, because 2 processes update it. So I made the buffer into a class and whenever there is code that affects it, I sandwich the code between ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.