Monday, July 14, 2014

How to make Classes thread safe

If we  have (a kind of) logging class. and this logging class will be used by many threads. How to make this class thread safe ? 
public static class SharedLogger : ILogger
{
   private static LogWriter _writer = new LogWriter();
   private static object _lock = new object();
   public static void Write(string s)
   {
       lock (_lock)
       {
           _writer.Write(s);
       }
   }
}
Thread - Safe Collections
http://stackoverflow.com/questions/9995266/how-to-create-a-thread-safe-generic-list}

Thread - Safe Collections 
Provided by .NET 4.0
http://www.codeproject.com/Articles/181410/Thread-safe-Collections-in-NET


 

No comments:

Post a Comment