2

我有一个多个用户可能会调用的过程。这是一个非常昂贵的查询,但它应该只需要每 5 分钟左右运行一次以刷新一些存档数据。我现在有一个锁,这样我们就不会一次运行该进程多次,这会使系统崩溃,但是每个用户都必须等待之前的锁运行才能运行。如果有 3-4 个用户在等待锁定,则第 4 个用户必须等待超过 20 分钟才能运行他们的查询。

我想做的是锁定这个对象并在第一个请求上运行查询。如果有任何其他请求进来,让它们等待当前锁完成,然后返回而不实际执行查询。

.Net 中是否有任何东西可以实现这一点,还是我需要为这个锁编写一些特定的代码?

4

2 回答 2

3

你可以用一个ManualResetEvent和一个锁来做到这一点。

private object _dataLock = new object();
private ManualResetEvent _dataEvent = new ManualResetEvent(false);

private ArchiveData GetData()
{
    if (Monitor.TryEnter(_dataLock))
    {
        _dataEvent.Reset();  // makes other threads wait on the data

        // perform the query

        // then set event to say that data is available
        _dataEvent.Set();
        try
        {
            return data;
        }
        finally
        {
            Monitor.Exit(_dataLock);
        }
    }

    // Other threads wait on the event before returning data.
    _dataEvent.WaitOne();
    return data;
}

所以第一个到达那里的线程获得锁并清除_dataEvent,表明其他线程将不得不等待数据。这里有一个竞争条件,如果第二个客户端在_dataEvent重置之前到达那里,它将返回旧数据。我认为这是可以接受的,考虑到它是存档数据并且发生这种情况的机会窗口非常小。

通过的其他线程尝试获取锁,失败并被WaitOne.

当数据可用时,执行查询的线程设置事件、释放锁并返回数据。

请注意,我没有将整个锁体放在try...finally. 请参阅 Eric Lippert 的Locks and Exceptions do not mix的原因。

于 2013-06-17T21:04:25.570 回答
2

此解决方案适用于那些不能接受多个调用者执行“准备代码”的可能性的人。

这种技术避免了在数据准备好的“正常”用例场景中使用锁。锁定确实有一些开销。这可能适用于您的用例,也可能不适用于您的用例。

该模式称为if-lock-if模式,IIRC。我试图尽我所能对内联进行注释:

bool dataReady;
string data;
object lock = new object();

void GetData()
{
    // The first if-check will only allow a few through. 
    // Normally maybe only one, but when there's a race condition 
    // there might be more of them that enters the if-block. 
    // After the data is ready though the callers will never go into the block, 
    // thus avoids the 'expensive' lock.
    if (!dataReady)
    {
        // The first callers that all detected that there where no data now
        // competes for the lock. But, only one can take it. The other ones
        // will have to wait before they can enter. 
        Monitor.Enter(lock);
        try
        {
            // We know that only one caller at the time is running this code
            // but, since all of the callers waiting for the lock eventually
            // will get here, we have to check if the data is still not ready.
            // The data could have been prepared by the previous caller,
            // making it unnecessary for the next callers to enter.
            if (!dataReady)
            { 
                // The first caller that gets through can now prepare and 
                // get the data, so that it is available for all callers.
                // Only the first caller that gets the lock will execute this code.
                data = "Example data";

                // Since the data has now been prepared we must tell any other callers that
                // the data is ready. We do this by setting the 
                // dataReady flag to true.
                Console.WriteLine("Data prepared!");
                dataReady = true;
            }
        }
        finally
        {
            // This is done in try/finally to ensure that an equal amount of 
            // Monitor.Exit() and Monitor.Enter() calls are executed. 
            // Which is important - to avoid any callers being left outside the lock. 
            Monitor.Exit(lock);
        }
    }

    // This is the part of the code that eventually all callers will execute,
    // as soon as the first caller into the lock has prepared the data for the others.
    Console.WriteLine("Data is: '{0}'", data);
}

MSDN 参考:

于 2016-06-21T11:34:21.553 回答