如何检测多线程使用?

时间:2014-06-04 20:51:03

标签: c# multithreading

在创建对象时和调用方法时比较ManagedThreadId是否足以验证它是否未在多线程场景中使用?

public class SingleThreadSafe
{
    private readonly int threadId;
    public SingleThreadSafe()
    {
        threadId = Thread.CurrentThread.ManagedThreadId;
    }

    public void DoSomethingUsefulButNotThreadSafe()
    {
        if(threadId!=Thread.CurrentThread.ManagedThreadId)
        {
            throw new InvalidOperationException(
                "This object is being accessed by a thread different than the one that created it. " +
                " But no effort has been made to make this object thread safe.");
        }
        //Do something useful, like use a previously established DbConnection
    }
}

我对穿线的直觉经常是错误的,所以我想检查一下我是否应该记住边缘情况。

3 个答案:

答案 0 :(得分:3)

不,这还不够!

CLR可以重用托管线程ID ,因此即使调用线程 不同,if(threadId!=Thread.CurrentThread.ManagedThreadId)也可以返回false一个用来构造对象。

您希望通过参考比较来实现目标:

if (!object.ReferenceEquals(Thread.CurrentThread, ThreadThatCreatedThis))
// ...

编辑:

然而

MSDN说:

  

ManagedThreadId属性的值不会随时间变化,即使承载公共语言运行库的非托管代码将线程实现为光纤。

http://msdn.microsoft.com/en-us/library/system.threading.thread.managedthreadid%28v=vs.110%29.aspx

答案 1 :(得分:1)

这基本上就是Windows Forms的功能。以下是来自Control.InvokeRequired的摘录(引自reference source):

public bool InvokeRequired {
    get {
        using (new MultithreadSafeCallScope())
        {
            ...
            int hwndThread = SafeNativeMethods.GetWindowThreadProcessId(hwnd, out pid);
            int currentThread = SafeNativeMethods.GetCurrentThreadId();
            return(hwndThread != currentThread);
        }
    }
}

如果比较线程ID对于Windows Forms来说已经足够了,我认为它对我来说已经足够了......

答案 2 :(得分:-1)

我担心它没有。

对于类似的情况,我使用ThreadStatic(或Thread Local Storage