-
Notifications
You must be signed in to change notification settings - Fork 0
Test3 #7
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: master
Are you sure you want to change the base?
Test3 #7
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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 |
| 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 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| FROM mcr.microsoft.com/dotnet/runtime:5.0 AS base | ||
| 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"] | ||
| 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!"); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Это не нужно |
||
| } | ||
| } | ||
| } | ||
| 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; | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Вообще, ждать и сигналить можно только захваченный замок, насколько я помню. Это ведь не WaitHandle. Два независимых замка друг с другом всё равно не синхронизируются. |
||
| } | ||
|
|
||
| value = _queue[1].value; | ||
| _queue.RemoveRange(1, 1); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. А вот несинхронизированные _queue.RemoveRange и _queue.Insert могут всё сломать: _queue — это List, который не даёт гарантий потокобезопасности, и тут мы обращаемся к его методам из-под разных lock-ов, которые никак не координируются друг с другом. Потенциальная гонка. |
||
| Volatile.Write(ref flag, false); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Так не получится отследить ситуацию, когда сразу несколько потоков ждут на Dequeue. Да и не нужно это, можно сигналить Monitor.PulseAll, даже если нас никто не ждёт |
||
| _size--; | ||
| return value; | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// возвращает размер | ||
| /// </summary> | ||
| public int Size() | ||
| => _size; | ||
| } | ||
| } | ||
| 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> |
| 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> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| using System.Linq; | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(() => | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Это не нужно |
||
| } | ||
| } | ||
| } | ||
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.
Вы теперь даже задачу контрольной в Docker собираете? :)