문제

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