Skip to content

Lazyy#2

Open
MikePuzanov wants to merge 9 commits intomasterfrom
Lazyy
Open

Lazyy#2
MikePuzanov wants to merge 9 commits intomasterfrom
Lazyy

Conversation

@MikePuzanov
Copy link
Copy Markdown
Owner

No description provided.

Copy link
Copy Markdown
Collaborator

@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.

Всё несколько сложнее, чем Вы думаете :) Там ещё где-то надо volatile

Comment thread Lazyy/Lazyy.Test/MultiThreadTest.cs Outdated
threads[i] = new Thread(() => lazyMulti.Get());
}

foreach (var thread in threads)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Suggested change
foreach (var thread in threads)
foreach (var thread in threads)

Comment thread Lazyy/Lazyy.Test/MultiThreadTest.cs Outdated
{
thread.Start();
}
foreach (var thread in threads)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Suggested change
foreach (var thread in threads)
foreach (var thread in threads)

Comment thread Lazyy/Lazyy/ILazy.cs Outdated
@@ -0,0 +1,7 @@
namespace Lazyy
{
public interface ILazy<T>
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Suggested change
public interface ILazy<T>
public interface ILazy<out T>

Comment thread Lazyy/Lazyy/ILazy.cs
{
public interface ILazy<T>
{
public T Get();

This comment was marked as resolved.

Comment thread Lazyy/Lazyy/LazyFactory.cs Outdated
/// </summary>
public class LazyFactory<T>
{
public static LazySingle<T> CreateSingleLazy(Func<T> supplier)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

И даже тут комментарии нужны

Comment thread Lazyy/Lazyy/LazyMulti.cs
lock (this._lockObject)
{
_isGenerate = true;
_value = _supplier();

This comment was marked as resolved.

This comment was marked as resolved.

Comment thread Lazyy/Lazyy/LazySingle.cs Outdated
namespace Lazyy
{
/// <summary>
/// реаилизация однопоточного режима
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Suggested change
/// реаилизация однопоточного режима
/// реализация однопоточного режима

Comment thread Lazyy/Lazyy/LazySingle.cs Outdated
private Func<T> _supplier;
private bool _isGenerate = false;


Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Suggested change

Comment thread Lazyy/Lazyy/LazyFactory.cs Outdated
/// <summary>
/// создает обьекты для работы либо в однопоточном, лио многопоточном режиме
/// </summary>
public class LazyFactory<T>
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Suggested change
public class LazyFactory<T>
public static class LazyFactory<T>

И параметр-тип ему не нужен, лучше методы генериками сделать. Тогда не надо будет указывать параметр-тип при вызове.

Comment thread Lazyy/Lazyy/Program.cs Outdated
{
thread.Join();
}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Suggested change

Comment thread Lazyy/Lazyy.Test/SingleThreadTest.cs Outdated
Assert.Throws<NullReferenceException>(() => LazyFactory.CreateSingleLazy<int>(null));
}
}
} No newline at end of file
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Нигде не проверяется, что если supplier null, происходит что-то разумное, что если supplier возвращает null, то тоже

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Хорошо, а если supplier возвращает null?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Не поправлено

Comment thread Lazyy/Lazyy/LazyFactory.cs Outdated
namespace Lazyy
{
/// <summary>
/// создает обьекты для работы либо в однопоточном, лио многопоточном режиме
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Suggested change
/// создает обьекты для работы либо в однопоточном, лио многопоточном режиме
/// создает обьекты для работы либо в однопоточном, либо в многопоточном режиме

Comment thread Lazyy/Lazyy/LazyMulti.cs Outdated
/// <summary>
/// создает обьект в многопоточном режиме
/// </summary>
public LazyMulti(Func<T> supplierNew)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Как-то странно параметр называется

Comment thread Lazyy/Lazyy/LazyMulti.cs
lock (this._lockObject)
{
_isGenerate = true;
_value = _supplier();

This comment was marked as resolved.

Copy link
Copy Markdown
Collaborator

@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.

Не-а :) Почти правильно, но всё равно есть гонка, и у Вас стремительно заканчиваются попытки

Comment thread Lazyy/Lazyy.Test/SingleThreadTest.cs Outdated
Assert.Throws<NullReferenceException>(() => LazyFactory.CreateSingleLazy<int>(null));
}
}
} No newline at end of file
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Хорошо, а если supplier возвращает null?

Comment thread Lazyy/Lazyy/LazyMulti.cs Outdated
namespace Lazyy
{
/// <summary>
/// реализация многопоточного режима
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Ну это не совсем правдивый комментарий. Это ведь не реализация pthreads или System.Threading :)

Comment thread Lazyy/Lazyy/LazyMulti.cs Outdated

_value = _supplier();
_supplier = null;
_isGenerate = true;
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

И тут наносит удар volatile и модель памяти многоядерных процессоров... _isGenerate = true другие потоки могут увидеть до _value = _supplier();, что приведёт к возврату null и крешу вызывающего.

Comment thread Lazyy/Lazyy/LazyMulti.cs Outdated
public class LazyMulti<T> : ILazy<T>
{
private T _value;
private bool _isGenerate = false;
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

_isGenerated?

Copy link
Copy Markdown
Collaborator

@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.

Окей, осталось только унифицировать тесты (уже не в счёт попыток). Все варианты Lazy реализуют один интерфейс и должны обладать одинаковым поведением на базовых сценариях (на null, на supplier, который возвращает null, что они не запускают вычисление дважды). Значит, можно написать один набор тестов и скармливать ему Lazy через TestCaseData (или, точнее, скармливать тестам функции, которые производят Lazy, можно прямо методы фабики --- ведь нам надо из теста им supplier передать). Тогда получится, что есть общие тесты на все Lazy, и один тест на многопоточную --- что гонок нет. А на однопоточную Lazy специфические тесты вообще не нужны, получается.

Comment thread Lazyy/Lazyy.Test/SingleThreadTest.cs Outdated
Assert.Throws<NullReferenceException>(() => LazyFactory.CreateSingleLazy<int>(null));
}
}
} No newline at end of file
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Не поправлено

Copy link
Copy Markdown
Collaborator

@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.

3 participants