archives

Thread-safe Singleton in C#

This has two parts. Please read first part first and think and then read second part.
+ First part:
Is this a thread-safe singleton class:

    1 public class SingletonClass

    2 {

    3     private SingletonClass () { Constructor (); }

    4     static SingletonClass instance;

    5     public static SingletonClass Instance

    6     {

    7         get

    8         {

    9             lock (typeof (SingletonClass))

   10             {

   11                 if (instance == null)

   12                     instance = new SingletonClass ();

   13                 lock (instance)

   14                 {

   15                     return instance;

   16                 }

   17             }

   18         }

   19     }

   20     void Constructor () { }

   21 }

--------------------------------------------------------------------------------

+ Second part:
I think this one is a thread-safe singleton:

    1 public class SingletonClass

    2 {

    3     public static object Hey_IAmStillUsingThisInstance_Lock = new object ();

    4     private SingletonClass () { Constructor (); }

    5     static SingletonClass instance;

    6     public static SingletonClass Instance

    7     {

    8         get

    9         {

   10             lock (typeof (SingletonClass))

   11             {

   12                 if (instance == null)

   13                     instance = new SingletonClass ();

   14                 lock (instance)

   15                 {

   16                     return instance;

   17                 }

   18             }

   19         }

   20     }

   21     void Constructor () { }

   22     public void DoSomthing () { }

   23 }

Which should be used this way:

  100 static void Main(string[] args)

  101 {

  102     lock (SingletonClass.Hey_IAmStillUsingThisInstance_Lock)

  103     {

  104         SingletonClass.Instance.DoSomthing ();

  105     }

  106 }

As you see it is a very very in-the-way kind of mistake! I did not beleive it when I came across it first time! The first one is thread safe just at the moment it's refrence is accessed by 'Instance' property not after that.
This filth is shared memory.
>:(

Declarative Assembler

Declarative Assembler. Christopher Kumar Anand, Jacques Carette, Wolfram Kahl, Cale Gibbard, Ryan Lortie. SQRL Technical Report #20, 2004.

As part of a larger project, we have built a declarative assembly language. This language enables us to specify multiple code paths to compute particular quantities, giving the instruction scheduler more flexibility in balancing execution resources for superscalar execution. The instruction scheduler is also innovative in that it includes aggressive pipelining, and exhaustive (but lazy) search for optimal instruction schedules. We present some examples where our approach has produced very promising results.

I think this paper is a nice followup to the recent discussion of SPE because it goes further than that paper by analyzing what data are necessary to achieve the ultimate goal of optimal or near-optimal instruction scheduling on superscalar architectures. In other words, it strongly suggests that we can do better than simply embedding low-level instructions in a high-level language by instead embedding a graph of desired results (vertices) and instructions for reaching them (edges) which can be analyzed to exploit cache-coherency, pipelining tricks, instruction-level parallelism, etc, or which could be supplied to an external scheduling algorithm based on some entirely different paradigm.

Status Report: HOT Pickles, and how to serve them

Status Report: HOT Pickles, and how to serve them, Andreas Rossberg, Guido Tack, and Leif Kornstedt. ML Workshop 2007.

The need for flexible forms of serialisation arises under many circumstances, e.g. for doing high-level inter-process communication or to achieve persistence. Many languages, including variants of ML, thus offer pickling as a system service, but usually in a both unsafe and inexpressive manner, so that its use is discouraged. In contrast, safe generic pickling plays a central role in the design and implementation of Alice ML: components are defined as pickles, and modules can be exchanged between processes using pickling. For that purpose, pickling has to be higher-order and typed (HOT), i.e. embrace code mobility and involve runtime type checks for safety. We show how HOT pickling can be realised with a modular architecture consisting of multiple abstraction layers for separating concerns, and how both language and implementation benefit from a design consistently based on pickling.

Newspeak, an experimental language in the style of Smalltalk

Languages that support both inheritance and nesting of declarations define method lookup to first climb up the inheritance hierarchy and then recurse up the lexical hierarchy. We discuss weaknesses of this approach, present alternatives, and illustrate a preferred semantics as implemented in Newspeak, a new language in the Smalltalk [GR83] family.
Gilad Bracha, On the Interaction of Method Lookup and Scope with Inheritance and Nesting (pdf)

... a standard example - a class representing points in the plane
Object Initialization and Construction Revisited