-
Notifications
You must be signed in to change notification settings - Fork 0
Lazy #2
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
base: main
Are you sure you want to change the base?
Conversation
Lazy/Lazy/Lazy/MultithreadedLazy.cs
Outdated
| /// <inheritdoc/> | ||
| public override T Get() | ||
| { | ||
| ///if the value has already been calculated, there should be no locks |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Тут обычный комментарий, не XMLDoc, так что
| ///if the value has already been calculated, there should be no locks | |
| // if the value has already been calculated, there should be no locks |
Lazy/Lazy/Lazy/MultithreadedLazy.cs
Outdated
| if (!Volatile.Read(ref isAlreadyCounted)) | ||
| { | ||
| value = func(); | ||
| isAlreadyCounted = true; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Надо ещё func занулить, чтобы сборщик мусора мог её собрать
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
А isAlreadyCounted = true бесполезно, потому что строчкой ниже мы делаем то же самое, только лучше
Lazy/Lazy/Lazy/MultithreadedLazy.cs
Outdated
| } | ||
| } | ||
|
|
||
| return value!; |
There was a problem hiding this comment.
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 (в условии это вроде предполагалось)
Lazy/Lazy/LazyTest/LazyTest.cs
Outdated
| { | ||
| static int Increment(int number) => number + 1; | ||
| Lazy.ILazy<int> lazy = new Lazy.MultithreadedLazy<int>(() => Increment(0)); | ||
| Thread[] threads = new Thread[8]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
var
Lazy/Lazy/LazyTest/LazyTest.cs
Outdated
| [Test] | ||
| public void ShouldValueNotChangeForLazyInMultithreadedMode() | ||
| { | ||
| static int Increment(int number) => number + 1; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Непоказательно. Из-за гонки по + 1 может получиться, что там все Lazy вызвались и посчитались, а ответ всё равно 1
Lazy/Lazy/LazyTest/LazyTest.cs
Outdated
| threads[i] = new Thread(() => { | ||
| Assert.That(lazy.Get(), Is.EqualTo(1)); | ||
| }); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Фигурные скобки лишние
yurii-litvinov
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
No description provided.