Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Interlocked Class (atomic operations)
//Rextester.Program.Main is the entry point for your code. Don't change it. //Compiler version 4.0.30319.17929 for Microsoft (R) .NET Framework 4.5 // //======================================================= // Code Examples from Exam Ref 70-483 //======================================================= // //AUTHOR: Wouter de Kort // //PURPOSE: Synchronizing Resources // //SECTION: Objective 1.2 - Chapter 1 // //MODIFICATION HISTORY //DATE NAME DESCRIPTION //----------- ---------------- -------------------- //10/19/2017 Cristian Canedo Initial commit //10/19/2017 Cristian Canedo Use lock keyword //10/19/2017 Cristian Canedo Use Interlocked class //======================================================= using System; using System.Threading; using System.Threading.Tasks; namespace Rextester { public class Program { ///<summary> /// EXPECTED: Ouput will always be 0 because there is no /// way one thread can change the value while the other /// thread is working with it /// **INTERLOCKED MAKES OPERATIONS ATOMIC** ///</summary> public static void Main(string[] args) { int number = 0; var up = Task.Run(() => { for (int i = 0; i < 1000000; i++) { Interlocked.Increment(ref number); } }); for (int i = 0; i < 1000000; i++){ Interlocked.Decrement(ref number); } up.Wait(); Console.WriteLine(number); } } }
run
|
edit
|
history
|
help
0
SumOfEvens
sdfrgthyjui65trfvf
Conversioni double
Enum, TryParse, cast to enum
test_hash
Hello world test
Armstrong
Fiddling around
Full join
Test