线程和C#中的静态方法 [英] Threading and static methods in C#

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

问题描述

下面是一个毫无意义的扩展方法为例:

Here is a meaningless extension method as an example:

public static class MyExtensions
{
    public static int MyExtensionMethod(this MyType e)
    {
        int x = 1;
        x = 2;

        return x
    }
}



说执行的线程完成高达并包括行:

Say a thread of execution completes upto and including the line:

x = 2; 



然后,处理器上下文切换,而另一个线程进入相同的方法,并完成行:

The processor then context switches and another thread enters the same method and completes the line:

int x = 1;



我是在假定变量x的创建,并通过第一线程分配的校正是在一独立的堆栈X创建,由第二分配,这意味着该变量此方法重入?

Am I correct in assuming that the variable "x" created and assigned by the first thread is on a separate stack to the variable "x" created and assigned by the second, meaning this method is re-entrant?

推荐答案

是的,每个线程都有自己独立的局部变量。这个函数总是返回2,即使叫同时被多个线程。

Yes, each thread gets its own separate local variable. This function will always return 2 even if called by multiple threads simultaneously.

这篇关于线程和C#中的静态方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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