C# Memory Model

C# Memory Model

==========================================

check this blog for more details : igoro.com.



Volatile Fields : - A read of a volatile field has acquire semantics which means it can't be reordered with subsequent operations
           - A write of a volatile field means that it cant be reordered with prior operations.

Atomicity :

Thread Communication Pattern:


The purpose of a of a memory model is Thread Communication .Communications such as when one thread writes something in memory and another thread reads from the memory , the memory
model dictates the values the reading thread might see.Locking is one way to ensure that thread don't land up reading dirty content.
Lock -- When a locked block of code executes its guaranteed to see all writes  from blocks that precede the block in the sequential order of the lock. Also it is guaranted to see any of the writes from the
block that follows it in sequential order of the lock.

Publication via Threading API:


======================================

class Threading
    {
        static int s_value;
        public static void run()
        {
            s_value = 42;
            Task T = Task.Factory.StartNew(() => { Console.WriteLine(s_value); });
            T.Wait();
        }


    }
}
In the above the StartNew API implementation ensure must ensure that  the write to s_value on thread1 will not move after <start task t> and the read from s_value on Thread2 will not move before <task t statring> .
Thread.Start and ThreadPool.QueueUserWorkItem also makes similar gurantees

Publication via Type Initialization

=========================================

Another way to safely publish a variable's value to is my declaring it static  in a static initializer or a static constructor. All the thread access is guaranted to get the same value.

class Threading
{
  static int s_value = 42;
  static object s_obj = new object();
  static void PrintValue()
  {
    Console.WriteLine(s_value);
    Console.WriteLine(s_obj == null);
  }
}

Lazy Initialization

======================================================
Lazy<T> is a class introduced in .NET 4.0 allows to initialize objects when we are going to utilize or assign to a object.
So in Lazy Initialization the object is declared and when the variables of the class gets value then only the object occupies space in the memory.
class Employee
    {
        public string name { get; set; }
        public int salary { get; set; }
        public int d{get;set;}
    }

    class program
    {
        public static void Main()
        {
            /* Stepes to initialize or create object only when it is required */
            /*Employee employee=null;
            Console.WriteLine(employee);
            if (employee == null)
            {
                employee = new Employee();
                Console.WriteLine(employee);
            }*/

            /* Stepes to initialize or create object only when it is required with Lazy Initializer */
            Lazy<Employee> emp=new Lazy<Employee>();
            Console.WriteLine(emp);
            emp.Value.name = "Mishra";
            emp.Value.salary = 3000;
            Console.WriteLine(emp);
            Console.ReadLine();

               
        }
    }

}


 C# will ensure that values are correctly aligned, but the user is able to override the alignment using the StructLayoutAttribute class

Comments

Popular posts from this blog

Authentication and Authorization in Web API -Part1

My Gardening Journey 6