KO CSharp Keyword lock - LIHACHTETAN/ClassicUO-Bad-Newbie-CSharp-Scripts-IDE GitHub Wiki
ะฃะบัะฐัะฝััะบะฐ ยท ะ ัััะบะธะน ยท English ยท Deutsch ยท Franรงais ยท Italiano ยท Espaรฑol ยท ็น้ซไธญๆ ยท ๆฅๆฌ่ช ยท ํ๊ตญ์ด
ํธ๋๋ถ C# ยท ์ค์ฉ ๊ฐ์ด๋
์ง์ ๋ ๊ฐ์ฒด ๋๋ System.Threading.Lock์์ ์ํธ ๋ฐฐ์ ๋ธ๋ก์ ์คํํฉ๋๋ค. ์ฐจ๋จ๋ ๋ธ๋ก์์๋ ๋๊ธฐ๋ฅผ ํ์ฉํ์ง ์์ต๋๋ค.
์ด ์์ ๋ฅผ Example.cs๋ก ๋ฐ๋ก ์ ์ฅํ๊ณ ์ด ํ์ด์ง์ ๋ค๋ฅธ ์์ ์ ๋
๋ฆฝ์ ์ผ๋ก ์คํํ์ธ์.
Example.cs:
// PROJECT-EXAMPLE
var gate = new object();
int count = 0;
lock (gate) { count++; }
System.Console.WriteLine(count);๊ฒฐ๊ณผ:
1
lock (gate) { ... }using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.IO;
using System.Runtime.InteropServices;
using System.Runtime.CompilerServices;
public static class Program
{
public static async Task Main(string[] args)
{
object gate = new(); int counter = 0;
Parallel.For(0, 20, _ => { lock (gate) { counter++; } });
Console.WriteLine(counter);
}
}using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.IO;
using System.Runtime.InteropServices;
using System.Runtime.CompilerServices;
public static class Program
{
public static async Task Main(string[] args)
{
System.Threading.Lock gate = new();
List<int> values = [];
Parallel.For(0, 10, i => { lock (gate) { values.Add(i); } });
Console.WriteLine(values.Count);
}
}๊ณต์ ์ถ์ฒ: Microsoft ํ์ต.