V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
FreeWong
V2EX  ›  编程

===Gemini、Copilot 都说有线程安全问题我说没有===

  •  
  •   FreeWong · 11 小时 34 分钟前 · 140 次点击

    以下 c#代码,不懂 c# 我也加了注释 我说以下代码并没有线程安全问题,但是我问了 Gemini 、Copilot 他们都说有问题

    namespace helloworld
    {
        internal class Program
        {
            static void Main()
            {
                int foo =0;
                //新建一个线程
                var t = new Thread(() => 
                {
                    foo++;
                } );
                t.Start(); //启动上面这个线程
                t.Join();  //主线程阻塞直到 t 线程完成
                Console.WriteLine(foo);
            }
        }
    
    }
    
    
    abelai
        1
    abelai  
       8 小时 11 分钟前
    建议直接问 foo++ 是否会有线程安全问题
    FreeWong
        2
    FreeWong  
    OP
       7 小时 47 分钟前
    @abelai foo++ 不是线程安全的。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   2864 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 23ms · UTC 15:09 · PVG 23:09 · LAX 07:09 · JFK 10:09
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.