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

Static instances

Hi,
I have been doing some work on an app, that has a particular requirement for an object that can only have a single instance active. A previous developer had simply created single static class.

private static MemoryStreamCreator memThing=new MemoryStreamCreator();

I thought that perhaps a Singleton pattern may have been more appropriate.

1. Is this sensible or is the code above ok? The only concern I had was that another developer in another class, within the same project could use the same line.

If it is, I was reading a blog

http://blogs.msdn.com/cjohnson/archi...10/152179.aspx

Which said creating an instance from within a Singleton was not thread safe. Can someone provide me with some code which will guarantee thread safety? I had tried to use the code from the above link, in this example but there appears to be an issue. I think it is related to the fact that the _synclock object is not static. Can someone explain the code supplied in the link.



public class Yada
{
private static MyFunkyObject _foo;
private Object _synclock = new Object();

public static MyFunkyObject Foo
{

get
{

lock(_synclock)
{
if(_foo == null)
_foo = new MyFunkyObject();
}

return _foo;
}
}
}

I have never had to to try to lock an object from a static method. I normally just use

lock(this)......

Also if creating the singleton is not threadsafe then am I better going with the original implementation?

Thanks

Nov 17 '05 #1
4 1251
John Jenkins wrote:
I thought that perhaps a Singleton pattern may have been more appropriate.

I have never had to to try to lock an object from a static method. I
normally just use

lock(this)......

Also if creating the singleton is not threadsafe then am I better going
with the original implementation?


Try this:

using System;
using System.Runtime.CompilerServices;

namespace Test
{
public class Singleton
{

private static Singleton s;

protected Singleton()
{
}

[MethodImpl(MethodImplOptions.Synchronized)]
public static UddiCache getInstance()
{
if (singleton == null)
{
s = new Singleton();
}
return s;
}
}
}

Jimbo

Nov 17 '05 #2
Jimbo wrote:
John Jenkins wrote:
I thought that perhaps a Singleton pattern may have been more
appropriate.

I have never had to to try to lock an object from a static method. I
normally just use

lock(this)......

Also if creating the singleton is not threadsafe then am I better
going with the original implementation?

Try this:

using System;
using System.Runtime.CompilerServices;

namespace Test
{
public class Singleton
{

private static Singleton s;

protected Singleton()
{
}

[MethodImpl(MethodImplOptions.Synchronized)]
public static UddiCache getInstance()
{
if (singleton == null)
{
s = new Singleton();
}
return s;
}
}
}

Jimbo


D'oh! In making the code generic I ****ed it up. The getInstance()
method should be:

public static Singleton getInstance()
{
if (s == null)
{
s = new Singleton();
}
return s;
}
Nov 17 '05 #3
This may help:

http://www.geocities.com/Jeff_Louie/OOP/oop4.htm
http://www.geocities.com/Jeff_Louie/...on_pattern.htm

At the IL level there is not a lot of difference between a singleton or
static call. If
you need the added level of indirection, subclassing or use of an
interface
contract, or access control then by all means use the singleton.

Regards,
Jeff

*** Sent via Developersdex http://www.developersdex.com ***
Nov 17 '05 #4
John Jenkins <jo**********@yahoo.com> wrote:
Hi, I have been doing some work on an app, that has a particular
requirement for an object that can only have a single instance
active. A previous developer had simply created single static class.

private static MemoryStreamCreator memThing=new MemoryStreamCreator();

I thought that perhaps a Singleton pattern may have been more appropriate.
That's a normal implementation for a Singleton though - it's not clear
to me what the previous developer had done...
1. Is this sensible or is the code above ok? The only concern I had
was that another developer in another class, within the same project
could use the same line.

If it is, I was reading a blog

http://blogs.msdn.com/cjohnson/archi...10/152179.aspx

Which said creating an instance from within a Singleton was not
thread safe.
No, it says that creating an instance without any locking isn't thread-
safe.
Can someone provide me with some code which will
guarantee thread safety?
See http://www.pobox.com/~skeet/csharp/singleton.html
I have never had to to try to lock an object from a static method. I
normally just use

lock(this)......


That's a bad idea too. See
http://www.pobox.com/~skeet/csharp/t...ckchoice.shtml

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #5

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

Similar topics

8
by: SJ | last post by:
Hi: I have a class which has a static member function. The function implements something common to all instances. How can the static member function know all of the (Get access to the instances'...
3
by: Mauzi | last post by:
hi, this may sound odd and noob like, but what is the 'big' difference between static and non-static funcitons ? is there any performace differnce? what is the best way to use them ? thnx ...
3
by: Jay | last post by:
Why are there static methods in C#. In C++ static was applied to data only (I believe) and it meant that the static piece of data was not a part of the object but only a part of the class (one...
4
by: Dave | last post by:
I used the following class and .aspx code below to understand how static works on variables and methods taken from...
9
by: Laban | last post by:
Hi, I find myself using static methods more than I probably should, so I am looking for some advice on a better approach. For example, I am writing an app that involves quite a bit of database...
11
by: Kevin Prichard | last post by:
Hi all, I've recently been following the object-oriented techiques discussed here and have been testing them for use in a web application. There is problem that I'd like to discuss with you...
8
by: crjjrc | last post by:
Hi, I've got a base class and some derived classes that look something like this: class Base { public: int getType() { return type; } private: static const int type = 0; };
4
by: Dave | last post by:
I have a global.asax file with Application_Start defined and create some static data there and in another module used in the asp.net application and I realize that static data is shared amongst...
5
by: pgrazaitis | last post by:
I cant seem to get my head wrapped around this issue, I have myself so twisted now there maybe no issue! Ok so I designed a class X that has a few members, and for arguments sake one of the...
5
by: Andy B | last post by:
I have a class that I want to make static but it uses some objects that are instance objects. I keep getting a compiler error saying something about using instance objects in a static class or...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
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...

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.