I\'m writing a special data structure that will be available in a .NET library and one of the features of this data structure is that is will be thread safe provided that only o
Rather than compare thread ID's you should store the ambient Thread in your class during construction.
class SingleThreadedClass
{
private Thread ownerThread;
public SingleThreadedClass()
{
this.ownerThread = Thread.CurrentThread;
}
public void Read(...)
{
if (Thread.CurrentThread != this.ownerThread)
throw new InvalidOperationException();
}
public void TakeOwnership()
{
this.ownerThread = Thread.CurrentThread;
}
}