开发者

C# Which thread pattern to choose

开发者 https://www.devze.com 2023-04-09 06:48 出处:网络
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:

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();
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号