Is it safe to call methods AutoResetEvent from different threads? Or should I avoid calls of the object from different threads?

class Test 
{
    EventWaitHandle wh = new AutoResetEvent(false);
    Thread worker = new(Work);

    public Test
    {
        worker.Start();
    }

    public void StopSignal()
    {
        wh.Set(); //<-- Main thread Call
    }

    void Work() 
    {
        wh.WaitOne(); //<-- Child thread call
    }
}
有帮助吗?

解决方案

Yes, it safe to call methods AutoResetEvent from different threads. Have a look at this link. I hope this makes you clear.

其他提示

That's fine - it's kind of what it's meant to be used for!

See this MSDN article for more info. There's a section called 'Thread Safety' near the bottom which is listed for most classes on MSDN, so if you're unsure about whether any other classes are thread safe in the future, you can just check there.

The entire point of AutoResetEvent and ManualResetEvent is exactly to provide synchronisation between different threads!

see This article for details.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top