我可以确定这个临时变量不会被优化掉吗?

发布于 2025-01-05 04:47:19 字数 438 浏览 0 评论 0原文

我的系统中工作线程上发生了一些事情。假设状态发生了变化。我想在 UI 线程中处理新的 state,因此我调度一个要在那里调用的委托:

var state = GetState();
Dispatcher.BeginInvoke(() => StateChanged(state));

StateChanged 在 UI 线程上执行时,我可以确定参数state的值是GetState()调度之前返回的值,或者是临时的state 变量被优化,以便 GetState()在 UI 线程上调用来填充 StateChanged 参数?

Something happens in my system on a worker thread. Let's say that the state changes. I want to handle the new state in the UI thread, so I dispatch a delegate to be invoked there:

var state = GetState();
Dispatcher.BeginInvoke(() => StateChanged(state));

When StateChanged executes on the UI thread, can I then be sure that the value of the parameter state is the value returned by GetState() before the dispatch, or will the temporary state variable be optimized away so that GetState() is called on the UI thread to populate the StateChanged parameter?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

划一舟意中人 2025-01-12 04:47:19

不,GetState 肯定会在工作线程上调用。在 lambda 表达式中移动 GetState() 调用的“优化”将完全无效。

No, GetState will definitely be called on the worker thread. An "optimization" to move the GetState() call within the lambda expression would be entirely invalid.

葬シ愛 2025-01-12 04:47:19

这不会发生,因为它会改变操作的语义,优化不允许这样做;您通过调用方法对变量进行赋值,因此由于 C# 不是惰性语言,因此此时必须发生这种情况。

This will not happen as it would change the semantics of the operations, optimizations are not allowed to do that; you make an assignment to a variable by calling a method, so because C# is not a lazy language this will have to happen at that point.

苍风燃霜 2025-01-12 04:47:19

你的代码

var state = GetState();
Dispatcher.BeginInvoke(() => StateChanged(state));

没问题。但这种变化将更难以分析和证明其正确性:

var state = GetState();
Dispatcher.BeginInvoke(() => StateChanged(state));  // after GetState()
state = null;                       // before or after StateChanged() ?

Your code

var state = GetState();
Dispatcher.BeginInvoke(() => StateChanged(state));

is fine. But this variation would be much more difficult to analyze and prove correct:

var state = GetState();
Dispatcher.BeginInvoke(() => StateChanged(state));  // after GetState()
state = null;                       // before or after StateChanged() ?
沦落红尘 2025-01-12 04:47:19

我可以确定参数状态的值就是这个值吗
GetState() 分派之前返回,或者临时状态变量是否会被优化>离开

状态不会被优化掉。
然而,之前实际上是这个问题的棘手部分。只要您之后(在同一范围内)不更改状态变量,您就无需担心。

{
 // say GetState returns 2
 var state = GetState(); 
 // state now = 2
 Dispatcher.BeginInvoke(() => StateChanged(state)); 
 state = 3; 
}

在上面的代码中,state 不会被优化掉。但是,这并不意味着 StateChanged 将以值 2 进行调用。如果工作线程在启动调度程序线程之前完成执行,则可能是 3。

这里的要点是变量捕获确保保留该值以供闭包使用,但这并不意味着该值是不可变的。

can I then be sure that the value of the parameter state is the value
returned by GetState() before the dispatch or will the temporary state variable be optimized > away

No state won't be optimized away.
However, Before is actually the tricky part about this question. As long as you are not changing the state variable after (in the same scope) you have nothing to worry about.

{
 // say GetState returns 2
 var state = GetState(); 
 // state now = 2
 Dispatcher.BeginInvoke(() => StateChanged(state)); 
 state = 3; 
}

In the code above state won't be optimized away. It does not mean, however, that StateChanged will be invoked with value 2. It could be 3, if the worker thread completes execution before starting the dispatcher thread.

The main point here is that variable capturing insures that the value is preserved for the use of the closure, but that does not mean that the value is immutable.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文