commit 0b30f2e1b6f469cf0304f0a0a10e356d562e0f36 Author: HiveBeats Date: Wed Feb 12 17:05:52 2025 +0300 Initial diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..cd967fc --- /dev/null +++ b/.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/.gitignore b/.gitignore new file mode 100644 index 0000000..add57be --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +bin/ +obj/ +/packages/ +riderModule.iml +/_ReSharper.Caches/ \ No newline at end of file diff --git a/Threading.sln b/Threading.sln new file mode 100644 index 0000000..3f43640 --- /dev/null +++ b/Threading.sln @@ -0,0 +1,16 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Threading", "Threading\Threading.csproj", "{C2139401-FCF7-45F0-BF27-443572B69D19}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {C2139401-FCF7-45F0-BF27-443572B69D19}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {C2139401-FCF7-45F0-BF27-443572B69D19}.Debug|Any CPU.Build.0 = Debug|Any CPU + {C2139401-FCF7-45F0-BF27-443572B69D19}.Release|Any CPU.ActiveCfg = Release|Any CPU + {C2139401-FCF7-45F0-BF27-443572B69D19}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection +EndGlobal diff --git a/Threading/Dockerfile b/Threading/Dockerfile new file mode 100644 index 0000000..87814f0 --- /dev/null +++ b/Threading/Dockerfile @@ -0,0 +1,21 @@ +FROM mcr.microsoft.com/dotnet/runtime:8.0 AS base +USER $APP_UID +WORKDIR /app + +FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build +ARG BUILD_CONFIGURATION=Release +WORKDIR /src +COPY ["Threading/Threading.csproj", "Threading/"] +RUN dotnet restore "Threading/Threading.csproj" +COPY . . +WORKDIR "/src/Threading" +RUN dotnet build "Threading.csproj" -c $BUILD_CONFIGURATION -o /app/build + +FROM build AS publish +ARG BUILD_CONFIGURATION=Release +RUN dotnet publish "Threading.csproj" -c $BUILD_CONFIGURATION -o /app/publish /p:UseAppHost=false + +FROM base AS final +WORKDIR /app +COPY --from=publish /app/publish . +ENTRYPOINT ["dotnet", "Threading.dll"] diff --git a/Threading/Program.cs b/Threading/Program.cs new file mode 100644 index 0000000..232b9dd --- /dev/null +++ b/Threading/Program.cs @@ -0,0 +1,9 @@ +// See https://aka.ms/new-console-template for more information + +using Threading; + +//Console.WriteLine("Hello, World!"); + +//await WaitThread.RunTaskBlocking(); + +await ShaRunner.RunMultiple(10000); \ No newline at end of file diff --git a/Threading/SHA256.cs b/Threading/SHA256.cs new file mode 100644 index 0000000..8c9637c --- /dev/null +++ b/Threading/SHA256.cs @@ -0,0 +1,39 @@ +using System.Security.Cryptography; +using System.Text; + +namespace Threading; + +public static class ShaRunner +{ + public static async Task RunMultiple(int count) + { + List> tasks = new List>(); + + for (int i = 0; i < count; i++) + { + var i1 = i; + tasks.Add(Task.Run(() => new SHA256ExampleBuggy().Calculate(i1))); + } + + var results = await Task.WhenAll(tasks); + Console.WriteLine($"Completed: {string.Join("\n", results)}"); + } +} + +public class SHA256ExampleBuggy +{ + private static readonly SHA256 _hash = SHA256.Create(); + + public string Calculate(int n) + { + string str = (n * 1000).ToString(); + Span data = stackalloc byte[Encoding.UTF8.GetByteCount(str)]; + Encoding.UTF8.GetBytes(str, data); + + Span buffer = stackalloc byte[32]; + _hash.TryComputeHash(data, buffer, out _); + + return Convert.ToBase64String(buffer); + } +} + diff --git a/Threading/Threading.csproj b/Threading/Threading.csproj new file mode 100644 index 0000000..4fa8577 --- /dev/null +++ b/Threading/Threading.csproj @@ -0,0 +1,17 @@ + + + + Exe + net8.0 + enable + enable + Linux + + + + + .dockerignore + + + + diff --git a/Threading/WaitThread.cs b/Threading/WaitThread.cs new file mode 100644 index 0000000..c099012 --- /dev/null +++ b/Threading/WaitThread.cs @@ -0,0 +1,68 @@ +namespace Threading; + +public class WaitThread +{ + public static void RunThread() + { + Console.WriteLine($"Main Thread {Thread.CurrentThread.ManagedThreadId} started: "); + var thr = new Thread(() => + { + Console.WriteLine($"Sleeping thread {Thread.CurrentThread.ManagedThreadId} started:"); + for (int i = 0; i < 10; i++) + { + Thread.Sleep(TimeSpan.FromSeconds(1)); + Console.WriteLine($"Number:{i}"); + } + Console.WriteLine($"Sleeping thread {Thread.CurrentThread.ManagedThreadId} ended"); + }); + thr.Start(); + + + //thr.Join(); + + Console.WriteLine($"Main Thread {Thread.CurrentThread.ManagedThreadId} ended."); + } + + public static async Task RunTaskNonBlocking() + { + Console.WriteLine($"Main Thread {Thread.CurrentThread.ManagedThreadId} started: "); + await Task.Run(async () => + { + Console.WriteLine($"Sleeping thread {Thread.CurrentThread.ManagedThreadId} started:"); + for (int i = 0; i < 10; i++) + { + await Task.Delay(TimeSpan.FromSeconds(1)); + Console.WriteLine($"Number:{i}"); + } + Console.WriteLine($"Sleeping thread {Thread.CurrentThread.ManagedThreadId} ended"); + }); + + + //thr.Join(); + + Console.WriteLine($"Main Thread {Thread.CurrentThread.ManagedThreadId} ended."); + } + + public static async Task RunTaskBlocking() + { + Console.WriteLine($"Main Thread {Thread.CurrentThread.ManagedThreadId} started: "); + await Task.Run(async () => + { + Console.WriteLine($"Sleeping thread {Thread.CurrentThread.ManagedThreadId} started:"); + for (int i = 0; i < 10; i++) + { + Thread.Sleep(TimeSpan.FromSeconds(1)); + //await Sleep(); + Console.WriteLine($"Number:{i}"); + } + Console.WriteLine($"Sleeping thread {Thread.CurrentThread.ManagedThreadId} ended"); + }); + + Console.WriteLine($"Main Thread {Thread.CurrentThread.ManagedThreadId} ended."); + } + + public static async Task Sleep() + { + Thread.Sleep(TimeSpan.FromSeconds(1)); + } +} \ No newline at end of file