In C#, any object can be used to protect a "critical section", or in other words, code that must not be executed by two threads at the same time.
For example, the following will synchronize access to the SharedLogger.Write method, so only a single thread is logging a message at any given time.
public class SharedLogger : ILogger
{
public static SharedLogger Instance = new SharedLogger();
public void Write(string s)
{
lock (_lock)
{
_writer.Write(s);
}
}
private SharedLogger()
{
_writer = new LogWriter();
}
private object _lock;
private LogWriter _writer;
}