Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Use Lock Keyword
Language:
Ada
Assembly
Bash
C#
C++ (gcc)
C++ (clang)
C++ (vc++)
C (gcc)
C (clang)
C (vc)
Client Side
Clojure
Common Lisp
D
Elixir
Erlang
F#
Fortran
Go
Haskell
Java
Javascript
Kotlin
Lua
MySql
Node.js
Ocaml
Octave
Objective-C
Oracle
Pascal
Perl
Php
PostgreSQL
Prolog
Python
Python 3
R
Rust
Ruby
Scala
Scheme
Sql Server
Swift
Tcl
Visual Basic
Layout:
Vertical
Horizontal
//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 //======================================================= 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 ///</summary> public static void Main(string[] args) { int number = 0; object _lock = new object(); var up = Task.Run(() => { for (int i = 0; i < 1000000; i++) { lock (_lock) number++; } }); for (int i = 0; i < 1000000; i++){ lock (_lock) number--; } up.Wait(); Console.WriteLine(number); } } }
Show compiler warnings
[
+
]
Show input
Compilation time: 0,12 sec, absolute running time: 0,13 sec, cpu time: 0,11 sec, average memory usage: 17 Mb, average nr of threads: 3
edit mode
|
history
|
discussion
-421008