Skip to content

Update memory.js to memory.cs #13

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 46 additions & 29 deletions JavaScript/memory.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,49 @@
'use strict';
using System;
using System.Threading;

const createPoint = (x, y) => {
const SIZE = 8;
const buffer = new SharedArrayBuffer(SIZE);
const view = new Int32Array(buffer);
view[0] = x;
view[1] = y;
const move = (dx, dy) => {
Atomics.add(view, 0, dx);
Atomics.add(view, 1, dy);
};
const clone = () => {
const x = Atomics.load(view, 0);
const y = Atomics.load(view, 1);
return createPoint(x, y);
};
const toString = () => {
const x = Atomics.load(view, 0);
const y = Atomics.load(view, 1);
return `(${x}, ${y})`;
};
return { move, clone, toString };
};
public class Point
{
private int x;
private int y;

// Usage
public Point(int _x, int _y)
{
x = _x;
y = _y;
}

const p1 = createPoint(10, 20);
console.log(p1.toString());
const c1 = p1.clone(p1);
c1.move(-5, 10);
console.log(c1.toString());
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());
}
}