尝试在睡眠循环中获取锁是否可以(避免死锁、饥饿等)?
我正在使用 ReaderWriterLock 类来锁定 Quotes 集合,它是一个 SortedDictionary。我正在考虑使用 while 循环,直到线程可以获得读取器锁,以防它暂时锁定写入。第一个问题,我的测试工作正常,但是这种方法有缺点吗?第二个问题,这样做的最佳/最佳实践方法是什么?
public void RequestQuote(string symbol, QuoteRequestCallback qrc)
{
// add the call back on a list and take care of it when the quote is available
while (!AcquireReaderLock(100)) Thread.Sleep(150);
if (Quotes.ContainsKey(symbol))
{
qrc(Quotes[symbol]);
rwl.ReleaseReaderLock();
}
else
{
rwl.ReleaseReaderLock();
lock (requestCallbacks)
requestCallbacks.Add(new KeyValuePair<string, QuoteRequestCallback>(symbol, qrc));
// request symbol to be added
AddSymbol(symbol);
}
}
private bool AquireReaderLock(int ms)
{
try
{
rwl.AcquireReaderLock(ms);
return true;
}
catch (TimeoutException)
{
return false;
}
}
private bool AquireWriterLock(int ms)
{
try
{
rwl.AcquireWriterLock(ms);
return true;
}
catch (TimeoutException)
{
return false;
}
}
I am using the ReaderWriterLock class to lock a Quotes collection which is a SortedDictionary. I am thinking of using a while loop until a thread can acquire the reader lock in case it is temporarily locked for writing. First question, my tests are working fine, but is there a drawback to this approach. Second question, what's the optimal/best-practice way of doing this?
public void RequestQuote(string symbol, QuoteRequestCallback qrc)
{
// add the call back on a list and take care of it when the quote is available
while (!AcquireReaderLock(100)) Thread.Sleep(150);
if (Quotes.ContainsKey(symbol))
{
qrc(Quotes[symbol]);
rwl.ReleaseReaderLock();
}
else
{
rwl.ReleaseReaderLock();
lock (requestCallbacks)
requestCallbacks.Add(new KeyValuePair<string, QuoteRequestCallback>(symbol, qrc));
// request symbol to be added
AddSymbol(symbol);
}
}
private bool AquireReaderLock(int ms)
{
try
{
rwl.AcquireReaderLock(ms);
return true;
}
catch (TimeoutException)
{
return false;
}
}
private bool AquireWriterLock(int ms)
{
try
{
rwl.AcquireWriterLock(ms);
return true;
}
catch (TimeoutException)
{
return false;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在使用
AcquireReaderLock()
方法锁定之前,您是否有第二段代码锁定requestCallbacks
?如果是这样,它可能会陷入僵局。添加循环和睡眠延迟以避免死锁在一般情况下不起作用。严格的锁获取层次结构将在一般情况下发挥作用。
Do you have a second piece of code locks
requestCallbacks
before locking with theAcquireReaderLock()
method? If so, it can deadlock with this.Adding loops and sleep delays to avoid deadlocks won't work in the general case. A strict hierarchy of lock acquisition will work in the general case.
我不太明白你想达到什么目的。 ReaderWriterLockSlim(我使用的)已经以这种方式运行,无需再多费吹灰之力 - 不需要额外的旋转,您只是在浪费资源。 a) 如果未找到符号,则将 R 锁升级为 W 锁,或者更好地将 R 和 W 访问分离到不同的方法中。 b) 不要提供从受保护部分内部调用未知代码的可能性
I don't quite understand what are you trying to achieve. The
ReaderWriterLockSlim
(which I'd use) already behaves that way without further ado - there is no need for additional spinning, you're just wasting resources. a) Promote the R-Lock to W-Lock if symbol is not found or better, separate R and W access into distinct methods. b) Don't provide the possibility to call into unknown code from inside of a protected section