I have a class:
public static class Message
{
private static readonly ReaderWriterLockSlim Locker = new ReaderWriterLockSlim();
private static string theMessage;
public static void SetMessage(string message)
{
Locker.EnterWriteLock();
theMessage = message;
Locker.ExitWriteLock();
}
public static string GetMessage()
{
Locker.EnterReadLock();
var msg = theMessage; // <<<=====
Locker.ExitReadLock();
return msg;
}
}
If I understand correctly, in the pointed line I'm creating a reference to theMessage
, and then returning it. Multiple threads will then access the same variable, or I'm wrong?
Do I need to call string.Copy
instead to make sure it is thread safe?
Thanks