I see in code of one big project such pattern:
我在一个大项目的代码中看到了这样的模式:
try {} finally { _someLock.EnterWriteLock(); }
try
{
...
}
finally
{
_someLock.ExitWriteLock();
}
Does it make in any sense to enter lock in finally?
在任何意义上它最终都会进入锁定状态吗?
===================
===================
Update: I found the source of such pattern.
更新:我找到了这种模式的来源。
http://chabster.blogspot.com/2013/07/a-story-of-orphaned-readerwriterlockslim.html . What can you say?
http://chabster.blogspot.com/2013/07/a-story-of-orphaned-readerwriterlockslim.html。你能说什么?
2 个解决方案
#1
2
No it doesn't. Empty try/finally block defends from excecuting finally even when the thread is aborted when it reaches try
block.
不,不。即使线程在到达try块时被中止,空try / finally块也会终止执行。
In fact it is harmful in your case.
事实上,它对您的情况有害。
Consider if thread abort exception is thrown when executing try
考虑在执行try时是否抛出线程中止异常
try
{
//Thread is here and abort requested
}
finally { _someLock.EnterWriteLock(); }//Aborted thread takes the lock!
try
{
...
}
finally
{
_someLock.ExitWriteLock();//Never gonna execute since thread is aborted. You're screwed.
}
#2
1
No, it does not. Maybe if you want to obfuscate your code
不,不是的。也许如果你想混淆你的代码
#1
2
No it doesn't. Empty try/finally block defends from excecuting finally even when the thread is aborted when it reaches try
block.
不,不。即使线程在到达try块时被中止,空try / finally块也会终止执行。
In fact it is harmful in your case.
事实上,它对您的情况有害。
Consider if thread abort exception is thrown when executing try
考虑在执行try时是否抛出线程中止异常
try
{
//Thread is here and abort requested
}
finally { _someLock.EnterWriteLock(); }//Aborted thread takes the lock!
try
{
...
}
finally
{
_someLock.ExitWriteLock();//Never gonna execute since thread is aborted. You're screwed.
}
#2
1
No, it does not. Maybe if you want to obfuscate your code
不,不是的。也许如果你想混淆你的代码