C# 代码虚拟化后返回空值

C# 代码虚拟化后返回空值,c#,.net,eazfuscator,C#,.net,Eazfuscator,我不知道这是一个bug还是我的错误?,在代码虚拟化后返回空值 [assembly: Obfuscation(Feature = "Apply to type *: apply to member * when method or constructor: virtualization", Exclude = false)] namespace ConsoleApp17 { class Program { private static bool valueWrit

我不知道这是一个bug还是我的错误?,在代码虚拟化后返回空值

[assembly: Obfuscation(Feature = "Apply to type *: apply to member * when method or constructor: virtualization", Exclude = false)]

namespace ConsoleApp17
{
    class Program
    {
        private static bool valueWritten = false;
        private static int sharedValue = 0;

        private static void ThreadOneStart()
        {
            sharedValue = 1000;
            valueWritten = true;
        }

        private static void ThreadTwoStart()
        {
            if (valueWritten) Console.Write(sharedValue == 1000 ? "Good" : "Bad");
        }

        static void Main(string[] args)
        {
            Thread threadOne = new Thread(ThreadOneStart);
            Thread threadTwo = new Thread(ThreadTwoStart);

            threadOne.Start();
            threadTwo.Start();

            threadOne.Join();
            threadTwo.Join();

            Console.ReadKey();
        }
    }
}

给定的程序具有竞争条件。这意味着程序行为未定义。它与eazfouscator.NET无关

以下是正确的方法:

[assembly: Obfuscation(Feature = "Apply to type *: apply to member * when method or constructor: virtualization", Exclude = false)]

class Program
{
    private static bool valueWritten = false;
    private static int sharedValue = 0;
    private static ManualResetEvent ev = new ManualResetEvent(false);

    private static void ThreadOneStart()
    {
        sharedValue = 1000;
        valueWritten = true;
        ev.Set();
    }

    private static void ThreadTwoStart()
    {
        ev.WaitOne();
        if (valueWritten) Console.Write(sharedValue == 1000 ? "Good" : "Bad");
    }

    static void Main(string[] args)
    {
        Thread threadOne = new Thread(ThreadOneStart);
        Thread threadTwo = new Thread(ThreadTwoStart);

        threadOne.Start();
        threadTwo.Start();

        threadOne.Join();
        threadTwo.Join();

        Console.ReadKey();
    }
}

在代码虚拟化后返回空值。
这在具体的特定术语中到底意味着什么呢当访问两个静态字段时没有线程同步,结果是未定义的。@KlausGütter代码混淆应该像原始代码一样工作。是的,但由于原始代码的行为未定义,模糊代码的行为也是如此。如果将静态布尔更改为合适的同步结构,例如
ManualResetEvent
,两者的工作原理是一样的。@DylanNicholson我认为编译器或CPU不禁止对两个赋值的执行进行重新排序。我在其他代码虚拟机的工作中尝试过,没有任何问题,因此不能说问题出在代码上。