C#
c# thread lock 명령어로 critical section 조절하기
데브웅
2021. 11. 7. 22:03
==코드==
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
namespace ConsoleApp5
{
class Program
{
static void Main(string[] args)
{
LockThread lockthread = new LockThread();
lockthread.Run();
}
}
class LockThread
{
private int counter = 1000;
private object obj = new object();
public void Run()
{
for (int i = 0; i < 10; i++)
{
new Thread(LockCalc).Start();
}
}
private void LockCalc()
{
lock (obj)
{
this.counter++;
for (int i = 0; i < this.counter; i++)
for (int j = 0; j < this.counter; j++)
{
}
Thread.Sleep(1000);
Console.WriteLine(this.counter);
}
}
}
}
|
cs |