-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathmemory.js
More file actions
49 lines (43 loc) · 1.14 KB
/
memory.js
File metadata and controls
49 lines (43 loc) · 1.14 KB
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
49
using System;
using System.Threading;
public class Point
{
private int x;
private int y;
public Point(int _x, int _y)
{
x = _x;
y = _y;
}
public void Move(int dx, int dy)
{
// Атомарне додавання (CAS-подібна операція)
Interlocked.Add(ref x, dx);
Interlocked.Add(ref y, dy);
}
public Point Clone()
{
// Атомарне читання значень
int currentX = Interlocked.CompareExchange(ref x, 0, 0);
int currentY = Interlocked.CompareExchange(ref y, 0, 0);
return new Point(currentX, currentY);
}
public override string ToString()
{
// Атомарне читання значень
int currentX = Interlocked.CompareExchange(ref x, 0, 0);
int currentY = Interlocked.CompareExchange(ref y, 0, 0);
return $"({currentX}, {currentY})";
}
}
class Program
{
static void Main()
{
var p1 = new Point(10, 20);
Console.WriteLine(p1.ToString());
var c1 = p1.Clone();
c1.Move(-5, 10);
Console.WriteLine(c1.ToString());
}
}