Lock语句如何确定授予对象访问权限的顺序? [英] How does the Lock statement determines the order of granting object access?

查看:112
本文介绍了Lock语句如何确定授予对象访问权限的顺序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要求一段代码的时间只能由一个线程运行(单个资源锁定).

I require a section of the code to be run by only one thread at time (single resource lock).

C#中的lock(object)语句允许这样做.但是,它不会保留对锁的请求顺序.

the lock(object) statement in C# allows this. It doesn't however, preserve the order of requests to the lock.

例如,考虑下面的100个线程起点,其中编号的线程按顺序尝试锁定挂锁:

For example, consider the 100 threadstarts below where numbered threads try to lock on padlock in order:

    for (int i = 0; i < 100; i++)
             {

            (new Thread(delegate(object index) 
               {
                  int name = (int) index;
                  byte[] len = new byte[2];

                  Console.WriteLine(string.Format("Thread:{0} going for lock. ",
                      name));
                  lock (padlock)
                  {

                     rnd.GetBytes(len);
                     ushort l = BitConverter.ToUInt16(len, 0);
                     Console.WriteLine(string.Format("Thread:{0} sleeping: {1}", 
                       name, l));
                     Thread.Sleep(l);
                  }
            })).Start(i);

实际授予访问权限的顺序不正确(1-> 100)或NOT FIFO.但是,似乎确实存在从头到尾"的EIEO模式(也许是由堆运行?).

The actual granting of the access is not in perfect order (1->100) or NOT FIFO. However there does seem to be an "early-in-early-out" EIEO pattern (run by a heap perhaps?).

问题是:是什么决定锁授予顺序,并且可以依靠不使不幸的线程饿死吗?

The Question is: What determines the lock granting order and can it be relied on not starving an unlucky thread?

更新:此答案对其进行了解释.这是相关的报价(在Windows上,Joe Duffy的并发编程):

Update: this answer explains it. Here is the pertinent quote (Joe Duffy's Concurrent Programming on Windows):

因为监视器内部使用内核对象,所以它们表现出相同的效果 OS同步机制还具有大致FIFO的行为 展示(在上一章中进行了介绍).监视器不公平,所以 如果另一个线程在唤醒等待之前尝试获取锁 线程尝试获取该锁,则允许偷偷摸摸的线程 获取锁.

Because monitors use kernel objects internally, they exhibit the same roughly-FIFO behavior that the OS synchronization mechanisms also exhibit (described in the previous chapter). Monitors are unfair, so if another thread tries to acquire the lock before an awakened waiting thread tries to acquire the lock, the sneaky thread is permitted to acquire a lock.

推荐答案

Servy的答案当然是正确的.其他一些细节:

Servy's answer is of course correct. A few additional details:

什么决定锁授予顺序?

What determines the lock granting order?

最终是操作系统.

是否可以依靠不使不幸的线程饿死?

Can it be relied on not starving an unlucky thread?

饥饿是不可能的,但可能的.如果您什至不能忍受很小的饥饿机会,那么您将需要比锁更复杂的东西.

Starvation is unlikely but possible. If you cannot abide even a small chance of starvation, you'll need something more complicated than a lock.

我还注意到,锁是不公平的",因为锁中可以有一个线程,等待中有八个线程,锁中的线程离开,而根本没有等待的第十个线程要求锁并获得它,有效地切断了界限". Joe对Windows为什么现在在这里使用不公平"的锁分配策略进行了有趣的分析,如果您对此主题感兴趣的话:

I note also that locks are "unfair" in that you can have one thread in the lock, eight threads waiting, the thread in the lock leaves, and a tenth thread that was not waiting at all asks for the lock and gets it, effectively "cutting the line". Joe gives an interesting analysis of why Windows now uses "unfair" lock allocation strategies here, if this subject interests you:

http://joeduffyblog.com/2006/12/14/anticonvoy-locks-in-windows-server-2003-sp1-and-windows-vista/

这篇关于Lock语句如何确定授予对象访问权限的顺序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆