From d41eb03f086b9cceba18403971a6688226887f93 Mon Sep 17 00:00:00 2001 From: MikePuzanov Date: Tue, 14 Dec 2021 17:00:17 +0300 Subject: [PATCH 1/3] add --- Test3/Test3/.dockerignore | 25 +++++++++++++ Test3/Test3/Dockerfile | 18 +++++++++ Test3/Test3/QueuePriory.cs | 76 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 119 insertions(+) create mode 100644 Test3/Test3/.dockerignore create mode 100644 Test3/Test3/Dockerfile create mode 100644 Test3/Test3/QueuePriory.cs diff --git a/Test3/Test3/.dockerignore b/Test3/Test3/.dockerignore new file mode 100644 index 0000000..cd967fc --- /dev/null +++ b/Test3/Test3/.dockerignore @@ -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 \ No newline at end of file diff --git a/Test3/Test3/Dockerfile b/Test3/Test3/Dockerfile new file mode 100644 index 0000000..3af2f58 --- /dev/null +++ b/Test3/Test3/Dockerfile @@ -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"] diff --git a/Test3/Test3/QueuePriory.cs b/Test3/Test3/QueuePriory.cs new file mode 100644 index 0000000..dd514a7 --- /dev/null +++ b/Test3/Test3/QueuePriory.cs @@ -0,0 +1,76 @@ +using System; +using System.Collections.Generic; +using System.Threading; + +namespace Test3 +{ + public class QueuePriory + { + private List<(int value, int priority)> _queue; + private readonly Object _lockAdded = new(); + private readonly Object _lockDelete = new(); + private int _size = 0; + private bool flag = false; + + public QueuePriory() + { + _queue = new List<(int, int)>(); + } + + 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++; + } + } + } + + public int Dequeue() + { + var value = 0; + lock (_lockDelete) + { + while (_size == 0) + { + Volatile.Write(ref flag, true); + Monitor.Wait(_lockAdded); + } + + value = _queue[1].value; + _queue.RemoveRange(1, 1); + Volatile.Write(ref flag, false); + _size--; + return value; + } + } + + public int Size() + => _size; + } +} From 97814048571a24a9e034bd9319d48cd87ef2fd77 Mon Sep 17 00:00:00 2001 From: MikePuzanov Date: Tue, 14 Dec 2021 17:01:18 +0300 Subject: [PATCH 2/3] add --- Test3/Test3.sln | 22 +++++++++++++++++ Test3/Test3/Program.cs | 12 +++++++++ Test3/Test3/Test3.csproj | 9 +++++++ Test3/TestQueue/TestQueue.csproj | 21 ++++++++++++++++ Test3/TestQueue/UnitTest1.cs | 42 ++++++++++++++++++++++++++++++++ 5 files changed, 106 insertions(+) create mode 100644 Test3/Test3.sln create mode 100644 Test3/Test3/Program.cs create mode 100644 Test3/Test3/Test3.csproj create mode 100644 Test3/TestQueue/TestQueue.csproj create mode 100644 Test3/TestQueue/UnitTest1.cs diff --git a/Test3/Test3.sln b/Test3/Test3.sln new file mode 100644 index 0000000..05e8eca --- /dev/null +++ b/Test3/Test3.sln @@ -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 diff --git a/Test3/Test3/Program.cs b/Test3/Test3/Program.cs new file mode 100644 index 0000000..ff1d6f2 --- /dev/null +++ b/Test3/Test3/Program.cs @@ -0,0 +1,12 @@ +using System; + +namespace Test3 +{ + class Program + { + static void Main(string[] args) + { + Console.WriteLine("Hello World!"); + } + } +} \ No newline at end of file diff --git a/Test3/Test3/Test3.csproj b/Test3/Test3/Test3.csproj new file mode 100644 index 0000000..a184b89 --- /dev/null +++ b/Test3/Test3/Test3.csproj @@ -0,0 +1,9 @@ + + + + Exe + net5.0 + Windows + + + diff --git a/Test3/TestQueue/TestQueue.csproj b/Test3/TestQueue/TestQueue.csproj new file mode 100644 index 0000000..dcb38d8 --- /dev/null +++ b/Test3/TestQueue/TestQueue.csproj @@ -0,0 +1,21 @@ + + + + net5.0 + enable + + false + + + + + + + + + + + + + + diff --git a/Test3/TestQueue/UnitTest1.cs b/Test3/TestQueue/UnitTest1.cs new file mode 100644 index 0000000..0548323 --- /dev/null +++ b/Test3/TestQueue/UnitTest1.cs @@ -0,0 +1,42 @@ +using System.Linq; +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(() => + { + 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(); + } + } +} \ No newline at end of file From 1520a5ae70002cfb238ac423d913a7c6301ac366 Mon Sep 17 00:00:00 2001 From: MikePuzanov Date: Tue, 14 Dec 2021 17:03:27 +0300 Subject: [PATCH 3/3] add --- Test3/Test3/QueuePriory.cs | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/Test3/Test3/QueuePriory.cs b/Test3/Test3/QueuePriory.cs index dd514a7..a576be5 100644 --- a/Test3/Test3/QueuePriory.cs +++ b/Test3/Test3/QueuePriory.cs @@ -4,6 +4,9 @@ namespace Test3 { + /// + /// потокобезопасная очередь с приоритетами + /// public class QueuePriory { private List<(int value, int priority)> _queue; @@ -17,6 +20,9 @@ public QueuePriory() _queue = new List<(int, int)>(); } + /// + /// добавление в очередь + /// public void Enqueue(int value, int priority) { var index = 1; @@ -51,6 +57,9 @@ public void Enqueue(int value, int priority) } } + /// + /// удаление из очереди + /// public int Dequeue() { var value = 0; @@ -70,6 +79,9 @@ public int Dequeue() } } + /// + /// возвращает размер + /// public int Size() => _size; }