简体   繁体   English

C#选择哪种线程模式

[英]C# Which thread pattern to choose

Say we have an interface method A, which is reentrant capable and for every entry in the method the current thread shall wait until an event occurs specifically for this thread: 假设我们有一个可重入的接口方法A,并且对于方法中的每个条目,当前线程应该等到特定于线程发生的事件:

void interfaceMethodA()
{
    doSomething();
    waitHandle.WaitOne();
}

Now, there will be set()-calls for the waitHandle, so that the method will be exited. 现在,将有set() - 调用waitHandle,以便退出该方法。 But those set() calls must release a specific thread of the (possible) thread queue and not neccessarily the first one. 但是那些set()调用必须释放(可能的)线程队列的特定线程,而不是必须释放第一个线程队列。 What is a best practise for this pattern, maybe wait() and pulse() in combination with a thread id vector? 这种模式的最佳实践是什么,也许wait()和pulse()结合线程id向量? To me this seems a bit like a mess... 对我来说这看起来有点乱......

Thanks in advance, Juergen 谢谢你,Juergen

You could use a ThreadLocal<WaitHandle> 您可以使用ThreadLocal<WaitHandle>

ThreadLocal<WaitHandle> waitHandle = new ThreadLocal<WaitHandle>(() => new ManualResetEvent(false));

void interfaceMethodA()
{
    doSomething();
    waitHandle.Value.WaitOne();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM