简体   繁体   English

名为EventWaitHandle的发布订单

[英]named EventWaitHandle release order

When AutoResetEvent calls WaitOne on Process 1 and it is called again in Process2, and Process 2 calls Set on the same AutoResetEvent, shouldn't it release the lock on Process1 as it was called before the one in Process 2? 当AutoResetEvent在进程1上调用WaitOne并在Process2中再次调用它,并且进程2在相同的AutoResetEvent上调用Set时,它不应该释放对Process1的锁定吗?

I have a sample code which I used to execute Process 1 and 2. I run Process 1 without any arguments, so it hangs at wh.WaitOne(). 我有一个示例代码,用于执行进程1和2。我在运行进程1时不带任何参数,因此它挂在wh.WaitOne()上。 I run Process 2 with an argument, so that it calls Set() What I observed is that Process 2's lock is released instead of Process1's. 我运行带有参数的Process 2,以便它调用Set()我观察到的是释放了Process 2的锁,而不是Process1的锁。

Should AutoResetEvent's lock queue behave as first in first out? AutoResetEvent的锁定队列是否应该表现为先进先出?

class ThreadSafe
{
    static EventWaitHandle wh = new EventWaitHandle(false, EventResetMode.AutoReset, "abc");

    static void Waiter(String t)
    {
            wh.WaitOne();
    }

    static void Main(string[] args)
    {
        new Thread(delegate() { Waiter("a"); }).Start();

        if (args.Length > 0)
        {
            wh.Set();
        }
    }
}

'Fairness' in locking is being deprecated, I could only find this link but I remember reading some more recent/relevant articles. 锁定中的“公平”已被弃用,我只能找到此链接,但我记得读过一些较新的/相关的文章。 If I recall correctly, all locking mechanisms in .NET are giving up FIFO behaviour. 如果我没记错的话,.NET中的所有锁定机制都将放弃FIFO行为。

The morale: you should not rely on FIFO order. 士气:您不应该依赖FIFO顺序。 Multi-threading is harder than it looks. 多线程比看起来难。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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