Skip to content

Lazy#2

Open
MinyazevR wants to merge 6 commits intomainfrom
Lazy
Open

Lazy#2
MinyazevR wants to merge 6 commits intomainfrom
Lazy

Conversation

@MinyazevR
Copy link
Copy Markdown
Owner

No description provided.

/// <inheritdoc/>
public override T Get()
{
///if the value has already been calculated, there should be no locks
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Тут обычный комментарий, не XMLDoc, так что

Suggested change
///if the value has already been calculated, there should be no locks
// if the value has already been calculated, there should be no locks

if (!Volatile.Read(ref isAlreadyCounted))
{
value = func();
isAlreadyCounted = true;
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Надо ещё func занулить, чтобы сборщик мусора мог её собрать

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

А isAlreadyCounted = true бесполезно, потому что строчкой ниже мы делаем то же самое, только лучше

}
}

return value!;
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

А вот плохо null-forgiving operator. Либо честно кидайте исключение, если null, либо официально разрешите значению быть null (в условии это вроде предполагалось)

{
static int Increment(int number) => number + 1;
Lazy.ILazy<int> lazy = new Lazy.MultithreadedLazy<int>(() => Increment(0));
Thread[] threads = new Thread[8];
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

var

[Test]
public void ShouldValueNotChangeForLazyInMultithreadedMode()
{
static int Increment(int number) => number + 1;
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Непоказательно. Из-за гонки по + 1 может получиться, что там все Lazy вызвались и посчитались, а ответ всё равно 1

Comment on lines +39 to +41
threads[i] = new Thread(() => {
Assert.That(lazy.Get(), Is.EqualTo(1));
});
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Фигурные скобки лишние

Copy link
Copy Markdown

@yurii-litvinov yurii-litvinov left a comment

Choose a reason for hiding this comment

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

👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants