473,396 Members | 2,093 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,396 software developers and data experts.

Singleton Thread-Safety Question

In the following implementation of Singleton, is the instance constructor
guaranteed to execute only once regardless of how many threads try to
access it at one time?

class Test {
public static readonly Test Instance = new Test();

Test() {
// set up state. this mustn't execute more than once
}
}
Nov 17 '05 #1
3 888
Cool Guy <co*****@abc.xyz> wrote:
In the following implementation of Singleton, is the instance constructor
guaranteed to execute only once regardless of how many threads try to
access it at one time?

class Test {
public static readonly Test Instance = new Test();

Test() {
// set up state. this mustn't execute more than once
}
}


Yes, that's guaranteed to only execute once. There are some caveats
about ordering when there's potential for a deadlock, but in simple
situations like the above (which 99% of static initializers fall into)
it's fine.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #2
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote:
class Test {
public static readonly Test Instance = new Test();

Test() {
// set up state. this mustn't execute more than once
}
}


Yes, that's guaranteed to only execute once. There are some caveats
about ordering when there's potential for a deadlock, but in simple
situations like the above (which 99% of static initializers fall into)
it's fine.


Could you give an example of one such caveat?
Nov 17 '05 #3
Cool Guy <co*****@abc.xyz> wrote:
Yes, that's guaranteed to only execute once. There are some caveats
about ordering when there's potential for a deadlock, but in simple
situations like the above (which 99% of static initializers fall into)
it's fine.


Could you give an example of one such caveat?


Sure. It's not actually a matter of initializing twice, but of the
incompletely initialized state being visible temporarily. For instance:

using System;

public class A
{
static string Something = B.Bar;
public static string Foo = "Hello";
}

public class B
{
public static string Bar = "There";

static B()
{
Console.WriteLine ("In B, A.Foo='{0}'", A.Foo);
}
}

public class Test
{
static void Main()
{
Console.WriteLine (A.Foo);
}
}

The results are:
In B, A.Foo=''
Hello

There are further rules for multithreaded system - I suggest you look
in the CLI spec, section 9.5.3.3 of partition 1 for further details.

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

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

Similar topics

16
by: cppaddict | last post by:
Hi, In this tutorial on singleton class in C++ (http://gethelp.devx.com/techtips/cpp_pro/10min/10min0200.asp) the author gives two implementations of a simple singleton class, claiming that...
1
by: Capstar | last post by:
Hi NG, I was reading about Singletons on devX. http://gethelp.devx.com/techtips/cpp_pro/10min/10min0200.asp This is their class: class Singleton { public: static Singleton* Instance();
13
by: William Stacey | last post by:
FYI. /// <summary> /// Author: William Stacey /// Fast and simple way to implement a singleton pattern without resorting /// to nested classes or other static vodo. Can also be easily converted...
4
by: Sgt. Sausage | last post by:
This may not be the best place to post this, but I'm coding in c# and, well this group is a "csharp" group! Background -- We encapsulate data access for an application through a home-grown...
16
by: ed_p | last post by:
Hello, I have implemented the singleton pattern for a class to hold a SqlConnection object that will be used thruout the application to create commands. My application is a simple Windows Form...
11
by: Eric | last post by:
I have a VB.net dll project with a class that is a singleton. I've been using this in winform apps without any problems. I would like to use this same dll in a web form project but my singleton...
2
by: Gabriele Farina | last post by:
Hi, I'm tring to implement a Singleton object that should be specific for every thread who create it, not global. I tried a solution that seems to work, but I have a very poor knowledge of...
4
by: Bit Byte | last post by:
Since the webserver loads a PHP script every time a request is issued from a client, it seems that Singletons are unnessesary in PHP?, since each time an object is invoked, it is (guaranteed?) to...
3
by: koredump | last post by:
I have inherited a Business Object architecture that makes heavy use of the Singleton design patter. For example the data access layer class is implemented as a static Singleton "object", there...
5
by: Lie | last post by:
This is probably unrelated to Python, as this is more about design pattern. I'm asking your comments about this design pattern that is similar in functionality to Singleton and Borg: to share...
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: 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
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?
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...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.