Skip to content
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;
Comment on lines +6 to +7
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it is better to have private fields with underscore _ prefix


// Usage
public Point(int _x, int _y)
{
x = _x;
y = _y;
}
Comment on lines +9 to +13
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While constructor parameters without underscore

Suggested change
public Point(int _x, int _y)
{
x = _x;
y = _y;
}
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);
Comment on lines +25 to +26
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use Volatile.Read for read (load)

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);
Comment on lines +33 to +34
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto

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());
}
}