分配函数值而不是使用 return 关键字及其对同步代码执行的影响
是否有区别
Public Function Foo() As Double
Return 3.0
End Function
我想知道 using和
Public Function Foo() As Double
Foo = 3.0
End Function
but 特别是在代码执行方面
。我正在尝试使用同步来管理多线程应用程序,并且不确定是否正确捕获了每个锁定和释放。
我知道“Return”之后的代码行不会执行,因为函数失去焦点,但是如果“Return”包含在 SyncLock 块中怎么办?
Public Function Foo() As Double
SyncLock fooLock
Return 3.0
End SyncLock
End Function
End SyncLock 是否被调用? SyncLock 块是简写吗:
Public Function Foo() As Double
Dim result as Double
Try
Threading.Monitor.Enter(fooLock)
result = 3.0
Finally
Threading.Monitor.Exit(fooLock)
End Try
Return result
End Function
如果我的理解是正确的,那么 Final 块在函数释放焦点之前出现,但如果 Final 等待 Return 和后续代码,那么可能需要一段时间才能有机会,即
Public Sub DoSomething()
Dim a As Double = Foo
...Do other things
End Sub
Public Function Foo() As Double
Try
Threading.Monitor.Enter(fooLock)
Return 3.0
...返回执行的代码,“a”被分配给 Foo 的返回值,然后线程上的所有其他任务可能都已完成,那么
Finally
Threading.Monitor.Exit(fooLock)
End Try
End Function
在这种情况下,我的锁可能持有时间太长。对于值类型,第一个代码是可接受的,但对于引用类型,第一个代码将释放锁,然后返回对对象的引用,并且使用者将对该值进行非同步访问,第二个代码可能会也可能不会工作,具体取决于函数中的中断之间执行了多少代码。
谁能帮我理清这些概念?
I am wondering if there is a difference between using
Public Function Foo() As Double
Return 3.0
End Function
and
Public Function Foo() As Double
Foo = 3.0
End Function
but specifically with respect to code execution.
I am attempting to manage a multithreaded application using synchronisation, and am not sure if I am capturing every lock and release correctly.
I understand that code lines after 'Return' are not executed because the function loses focus, but what if the 'Return' is wrapped in a SyncLock block?
Public Function Foo() As Double
SyncLock fooLock
Return 3.0
End SyncLock
End Function
Does the End SyncLock get called? Is the SyncLock block shorthand for:
Public Function Foo() As Double
Dim result as Double
Try
Threading.Monitor.Enter(fooLock)
result = 3.0
Finally
Threading.Monitor.Exit(fooLock)
End Try
Return result
End Function
If my understanding is correct then the Finally block comes before the function releases focus, but alternatively if the Finally waits on the Return and subsequent code, then it may be a while before the Finally gets a chance, i.e.
Public Sub DoSomething()
Dim a As Double = Foo
...Do other things
End Sub
Public Function Foo() As Double
Try
Threading.Monitor.Enter(fooLock)
Return 3.0
...code returned to executes, 'a' is assigned to the return value of Foo, then perhaps all of the other tasks on the thread are done, then
Finally
Threading.Monitor.Exit(fooLock)
End Try
End Function
In this case my lock may have held for too long. For value types the first code would be acceptible, but for reference types, the first would release the lock then return a reference to the object and the consumer would have non-synced access to the value, the second may or may not work, depending on how much code is executed inbetween the break in the function.
Could anyone help me straighten these concepts out?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
RETURN 3 和 v = 3 Return X 之间肯定有区别,
Return X 会立即终止调用,但它肯定会贯穿您可能打开的任何 try catch finally 。
v = 3 只是将返回值设置为 3,但不返回。函数中继续执行,直到函数结束或退出函数。
我对同步锁问题不是 100% 确定,但我打赌 RETURNing 会正确终止它。
There is definitely a difference between RETURN 3 and v = 3
Return X terminates the call right there, but it definitely runs through any try catch finally's you might have open.
v = 3 simply sets up the return value as 3, but does not return. Execution continues on in the function until the end of function or an exit function.
I'm not 100% sure about the synclock question, but I'd wager than RETURNing out of it would terminate it properly.
请不要再使用“为函数名称分配一个值并返回”模式。对于我们中的一些人来说,它与
REM
一样。也就是说,如果您查看使用 return 与 allocate 函数名称的代码生成的 IL,您会发现它们 100% 相同。
至于您的其他问题,根据 MSDN:
Please don't use the "assign function name a value and return" pattern anymore. Its up there with
REM
for some of us.That said, if you look at the IL generated from code that uses the return vs assign function name you'll see that it is 100% the same.
As for your other question, according to MSDN: