本文介绍了发生senerio种族的情况?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 发生senerio种族情况?用示例演示。At which senerio race condition occur ? Demonstrate with example.推荐答案 long x = 0;var task1 = Task.Run( ( ) => { for( int i = 0; i < 10000000; ++i ) ++x; } );var task2 = Task.Run( ( ) => { for( int j = 0; j < 10000000; ++j ) --x; } );task1.Wait();task2.Wait();Console.WriteLine( x );   第一个增加 X ;第二个减少它。完成任务后,预计在 x 中获得0。但是,有时 x 在执行'++ x'和'--x'时由于"竞争条件"而产生意外结果。 The first one increments the x; the second one decrements it. After finishing the tasks, it is expected to obtain 0 inx. However sometimes x has an unexpected result due to "race condition" while performing ‘++x’ and ‘--x’. 要解决此问题,请使用 Interlocked 类或 lock 。 To fix the problem, use Interlocked class or lock. 这篇关于发生senerio种族的情况?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-28 18:36