Skip to content

Lazy#50

Open
IgnatSergeev wants to merge 6 commits intomainfrom
lazy
Open

Lazy#50
IgnatSergeev wants to merge 6 commits intomainfrom
lazy

Conversation

@IgnatSergeev
Copy link
Owner

No description provided.

/// </summary>
/// <returns>Function result</returns>
/// <exception cref="Exception">If delegate threw exception</exception>
public T Get()

Choose a reason for hiding this comment

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

Тут и в другой реализации компилятор недоволен nullability-анализом, надо исправить

/// <summary>
/// Delegate defining the lazy
/// </summary>
private readonly Func<T> _supplier;

Choose a reason for hiding this comment

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

Нужен перенос полей (см. Умножение матриц)

/// </summary>
/// <returns>Function result</returns>
/// <exception cref="Exception">If delegate threw exception</exception>
public T Get()

Choose a reason for hiding this comment

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

Здесь и в многопоточной реализации стоит еще supplier после вычисления занулить, чтобы сборщик мусора смог собрать объект

{
public int CallCounter;

public int FstTest()

Choose a reason for hiding this comment

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

Этим функциям стоит присвоить более осмысленные и говорящие имена, хоть они и для тестов нужны

}
}

private static IEnumerable<TestCaseData> IntLazyImpl()

Choose a reason for hiding this comment

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

Это не домашка на Rust :)
Так что impl -> implementation


private class LazyTester
{
public int CallCounter;

Choose a reason for hiding this comment

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

Похоже { get; } затерялось :)
Иначе это public-поле

var tester = new LazyTester();
var lazy = new MultiThreadedLazy<int>(tester.FstTest);

var threads = new Thread[4];

Choose a reason for hiding this comment

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

Да можно и потоков 10 создать, чего мелочиться :)

Assert.Multiple(() =>
{
Assert.That(lazy.Get(), Is.EqualTo(1));
Assert.That(tester.CallCounter, Is.EqualTo(1));

Choose a reason for hiding this comment

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

Стоит добавить в тест ManualResetEvent, чтобы увеличить вероятность того, что все потоки отправятся получать значение в одно и то же время

Copy link

@YuriUfimtsev YuriUfimtsev Oct 11, 2023

Choose a reason for hiding this comment

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

Кстати, CallCounter тогда volatile должен быть. А лучше вообще в функциях ++ на Interlocked.Increment заменить и с volatile не возиться

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