多线程和.NET关闭 [英] Multithreading and closures in .NET

查看:243
本文介绍了多线程和.NET关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有这样的:

public string DoSomething(string arg)
{
    string someVar = arg;
    DoStuffThatMightTakeAWhile();
    return SomeControl.Invoke(new Func<string>(() => someVar));
}

和这种方法可以同时从多个线程调用,并且一个线程是停留在 DoStuffThatMightTakeAWhile ,然后第二个线程调用 DoSomething的用不同的 ARG ,将这种改变 someVar 值的所有线程,因此,的DoSomething 返回两个呼叫的第二个版本 someArg ,或者将一个 someVar 存在每个线程?

And this method can be called concurrently from multiple threads, and one thread is stuck at DoStuffThatMightTakeAWhile, and then a second thread calls DoSomething with a different arg, will this change the value of someVar for all threads and consequently, DoSomething return the second version of someArg on both calls, or will one someVar exist for each thread?

修改我想我的动作应该是一个 Func键所以编辑它。

Edit I think my Action should have been a Func so edited it.

推荐答案

有一些混乱的在这里的答案,大多基于所述不真实本地变量被分配的线程的堆栈上的。这既是错误的和不相关的。

There are a number of confusions in the answers here, mostly based upon the untruth that local variables are allocated "on the stack of the thread". This is both false and irrelevant.

这是假,因为局部变量可以被分配在一些临时游泳池或长期存储池;即使它被分配在临时池,即不需要堆栈存储器;它可能是一个寄存器。这无关紧要,因为谁在乎池的存储分配上?

It's false because the local variable may be allocated on some temporary pool or the long-term storage pool; even if it is allocated on a temporary pool, that need not be stack memory; it could be a register. It's irrelevant because who cares what pool the storage is allocated on?

相关事实是,局部变量每个方法激活分配一个顶级更一般地,在一个块中的局部变量是每个被输入块分配一次。在一个循环体中声明的局部变量,例如,在每次循环善有善报时间分配。

The relevant fact is that a top-level local variable is allocated per method activation. More generally, a local variable in a block is allocated once per the block being entered; a local variable declared in the body of a loop, for example, is allocated every time the loop goes around.

那么,让我们考虑你的问题:

So, let's consider your question:

此方法可以同时从多个线程调用。如果一个线程是停留在DoStuffThatMightTakeAWhile,然后一个第二线程调用DoSomething的与不同的精氨酸,将这个改变someVar的值对于所有线程?

This method can be called concurrently from multiple threads. If one thread is stuck at DoStuffThatMightTakeAWhile, and then a second thread calls DoSomething with a different arg, will this change the value of someVar for all threads?

没有。有一个新的someVar为的每个激活的DoSomething的了。

No. There is a new "someVar" for each activation of DoSomething.

会不会有someVar存在每个线程?

will one someVar exist for each thread?

一someVar将存在于每个的激活的。如果一个线程不正是一个激活然后会有每个线程只有一个someVar;如果一个线程执行一百万次,然后就会有一百万个。

One "someVar" will exist for each activation. If a thread does exactly one activation then there will be exactly one someVar per thread; if a thread does a million activations then there will be a million of them.

不过,乔恩·汉娜的回答也是正确的:如果你犯了一个委托其读取和写入一个局部变量,而你的手的该委托的出多个线程,那么所有激活的委托的的共享同一个局部变量。这里是为您创建没有神奇的线程安全;如果你想这样做,那么你有责任确保线程安全的。

That said, Jon Hanna's answer is also correct: if you make a delegate which both reads and writes a local variable, and you hand that delegate out to multiple threads, then all the activations of the delegate share the same local variable. There is no magical thread safety created for you; if you want to do that then you are responsible for ensuring thread safety.

这篇关于多线程和.NET关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆