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:
void interfaceMethodA()
{
doSomething();
waitHandle.WaitOne();
}
Now, there will be set()-calls for the waitHandle, so that the method will be exited. 开发者_JAVA百科But those set() calls must release a specific thread of the (possible) thread queue and not neccessarily the first one. What is a best practise for this pattern, maybe wait() and pulse() in combination with a thread id vector? To me this seems a bit like a mess...
Thanks in advance, Juergen
You could use a ThreadLocal<WaitHandle>
ThreadLocal<WaitHandle> waitHandle = new ThreadLocal<WaitHandle>(() => new ManualResetEvent(false));
void interfaceMethodA()
{
doSomething();
waitHandle.Value.WaitOne();
}
精彩评论