Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# - Static readonly strings -- possible to run into multithread issues?

public class MyClass<T>
{
    public static readonly String MyStringValue;

    static MyClass()
    {
        MyStringValue = GenerateString();
    }

    private static String GenerateString()
    {
        //Dynamically generated ONCE per type (hence, not const)
    }

    public void Foo()
    {
        Console.WriteLine(MyStringValue);
    }
}

It's my understanding that the static readonly String won't get generated until the static constructor is called on the class. But, the static constructor won't be called until one of the static methods or variables is accessed.

In a multi-threaded environment is it possible to run into issues because of this? Basically, is the static constructor by default singleton locked or do I have to do this myself? That is... do I have to do the following:

private static Object MyLock;

static MyClass()
{
    lock(MyLock)
    {
        if (MyStringValue == null)
            MyStringValue = GenerateString();
    }
}
like image 344
michael Avatar asked Jun 16 '11 19:06

michael


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr. Stroustroupe.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

How old is the letter C?

The letter c was applied by French orthographists in the 12th century to represent the sound ts in English, and this sound developed into the simpler sibilant s.


2 Answers

The static constructor is guaranteed to run only once per instantiated type. So you don't need your locking.

Note that it will run once for each generic parameter. And the static fields on the generic class aren't shared between different generic parameters either.

like image 110
CodesInChaos Avatar answered Sep 30 '22 00:09

CodesInChaos


To avoid this why not make the value a static property with only a get accessor which returned the cached value, which you could then make private? Accessing the property get would assure the static constructor ran first.

like image 22
Ed Bayiates Avatar answered Sep 30 '22 02:09

Ed Bayiates