简体   繁体   中英

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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