Skip to content
Open
Show file tree
Hide file tree
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
22 changes: 22 additions & 0 deletions Test3/Test3.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

Microsoft Visual Studio Solution File, Format Version 12.00
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Test3", "Test3\Test3.csproj", "{343EA1D0-30F4-442D-9D43-A53DAF81EF29}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestQueue", "TestQueue\TestQueue.csproj", "{AB6B0736-EF18-424C-A5F7-B21772DE1D18}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{343EA1D0-30F4-442D-9D43-A53DAF81EF29}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{343EA1D0-30F4-442D-9D43-A53DAF81EF29}.Debug|Any CPU.Build.0 = Debug|Any CPU
{343EA1D0-30F4-442D-9D43-A53DAF81EF29}.Release|Any CPU.ActiveCfg = Release|Any CPU
{343EA1D0-30F4-442D-9D43-A53DAF81EF29}.Release|Any CPU.Build.0 = Release|Any CPU
{AB6B0736-EF18-424C-A5F7-B21772DE1D18}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{AB6B0736-EF18-424C-A5F7-B21772DE1D18}.Debug|Any CPU.Build.0 = Debug|Any CPU
{AB6B0736-EF18-424C-A5F7-B21772DE1D18}.Release|Any CPU.ActiveCfg = Release|Any CPU
{AB6B0736-EF18-424C-A5F7-B21772DE1D18}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal
25 changes: 25 additions & 0 deletions Test3/Test3/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
**/.dockerignore
**/.env
**/.git
**/.gitignore
**/.project
**/.settings
**/.toolstarget
**/.vs
**/.vscode
**/.idea
**/*.*proj.user
**/*.dbmdl
**/*.jfm
**/azds.yaml
**/bin
**/charts
**/docker-compose*
**/Dockerfile*
**/node_modules
**/npm-debug.log
**/obj
**/secrets.dev.yaml
**/values.dev.yaml
LICENSE
README.md
18 changes: 18 additions & 0 deletions Test3/Test3/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
FROM mcr.microsoft.com/dotnet/runtime:5.0 AS base
Copy link
Collaborator

Choose a reason for hiding this comment

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

Вы теперь даже задачу контрольной в Docker собираете? :)

WORKDIR /app

FROM mcr.microsoft.com/dotnet/sdk:5.0 AS build
WORKDIR /src
COPY ["Test3/Test3.csproj", "Test3/"]
RUN dotnet restore "Test3/Test3.csproj"
COPY . .
WORKDIR "/src/Test3"
RUN dotnet build "Test3.csproj" -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "Test3.csproj" -c Release -o /app/publish

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "Test3.dll"]
12 changes: 12 additions & 0 deletions Test3/Test3/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System;

namespace Test3
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
Copy link
Collaborator

Choose a reason for hiding this comment

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

Это не нужно

}
}
}
88 changes: 88 additions & 0 deletions Test3/Test3/QueuePriory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
using System;
using System.Collections.Generic;
using System.Threading;

namespace Test3
{
/// <summary>
/// потокобезопасная очередь с приоритетами
/// </summary>
public class QueuePriory
{
private List<(int value, int priority)> _queue;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Стоило сделать её генериком, хранить только инты не очень полезно

private readonly Object _lockAdded = new();
private readonly Object _lockDelete = new();
private int _size = 0;
private bool flag = false;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Флаг чего? Плохое имя переменной


public QueuePriory()
{
_queue = new List<(int, int)>();
}

/// <summary>
/// добавление в очередь
/// </summary>
public void Enqueue(int value, int priority)
{
var index = 1;
lock (_lockAdded)
{
if (_size == 0 && !flag)
{
_queue.Insert(0, (value, priority));
_size++;
//Monitor.PulseAll(_lockDelete);
return;
}

if (flag)
{
_queue.Insert(0, (value, priority));
_size++;
Monitor.PulseAll(_lockDelete);
return;
}
foreach (var t in _queue)
{
if (priority > t.priority)
{
_queue.Insert(index, (value, priority));
_size++;
return;
}

index++;
}
}
}

/// <summary>
/// удаление из очереди
/// </summary>
public int Dequeue()
{
var value = 0;
lock (_lockDelete)
{
while (_size == 0)
{
Volatile.Write(ref flag, true);
Monitor.Wait(_lockAdded);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Вообще, ждать и сигналить можно только захваченный замок, насколько я помню. Это ведь не WaitHandle. Два независимых замка друг с другом всё равно не синхронизируются.

}

value = _queue[1].value;
_queue.RemoveRange(1, 1);
Copy link
Collaborator

Choose a reason for hiding this comment

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

А вот несинхронизированные _queue.RemoveRange и _queue.Insert могут всё сломать: _queue — это List, который не даёт гарантий потокобезопасности, и тут мы обращаемся к его методам из-под разных lock-ов, которые никак не координируются друг с другом. Потенциальная гонка.

Volatile.Write(ref flag, false);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Так не получится отследить ситуацию, когда сразу несколько потоков ждут на Dequeue. Да и не нужно это, можно сигналить Monitor.PulseAll, даже если нас никто не ждёт

_size--;
return value;
}
}

/// <summary>
/// возвращает размер
/// </summary>
public int Size()
=> _size;
}
}
9 changes: 9 additions & 0 deletions Test3/Test3/Test3.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net5.0</TargetFramework>
<DockerDefaultTargetOS>Windows</DockerDefaultTargetOS>
</PropertyGroup>

</Project>
21 changes: 21 additions & 0 deletions Test3/TestQueue/TestQueue.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.11.0" />
<PackageReference Include="NUnit" Version="3.13.2" />
<PackageReference Include="NUnit3TestAdapter" Version="4.0.0" />
<PackageReference Include="coverlet.collector" Version="3.1.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Test3\Test3.csproj" />
</ItemGroup>

</Project>
42 changes: 42 additions & 0 deletions Test3/TestQueue/UnitTest1.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using System.Linq;
Copy link
Collaborator

Choose a reason for hiding this comment

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

UnitTest1.cs как имя файла не очень, и не совпадает с именем класса внутри.

using System.Threading;
using System.Threading.Tasks;
using NUnit.Framework;
using Test3;

namespace TestQueue
{
public class Tests
{
[Test]
public void Test1()
{
var array = new (int, int)[50];
for (int i = 1; i < 51; ++i)
{
array[i - 1] = (51 - i, i);
}
var count = 0;
var queue = new QueuePriory();
var threads = new Thread[5];

for (int i = 0; i < threads.Length; ++i)
{
var localI = i;
Task.Run(() =>
Copy link
Collaborator

Choose a reason for hiding this comment

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

Так Вы теряете ссылку на задачу, и может оказаться, что не даёте ей доработать. У меня вот тест не проходит, потому что к 37-й строке размер очереди 49, а надо 50 — одна из задач не закончила одну итерацию цикла :)

{
for (int j = localI; j < 50; j += 5)
{
queue.Enqueue(array[j].Item1, array[j].Item2);
}
}
);
}

array = array.Reverse().ToArray();
Assert.AreEqual(50, queue.Size());
Assert.AreEqual(array[0].Item1, queue.Dequeue());
Assert.Pass();
Copy link
Collaborator

Choose a reason for hiding this comment

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

Это не нужно

}
}
}