thread sync

main
syneffort 2 years ago
parent 3c54eb1d0f
commit 4aa115a3d9
  1. 6
      MultiThread/MultiThread.sln
  2. 62
      MultiThread/ThreadSyncTest/Program.cs
  3. 10
      MultiThread/ThreadSyncTest/ThreadSyncTest.csproj

@ -7,6 +7,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Server", "Server\Server.csp
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Client", "Client\Client.csproj", "{A7321FF5-0046-4C28-BFE6-DE87E6C8E60C}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ThreadSyncTest", "ThreadSyncTest\ThreadSyncTest.csproj", "{525F636D-8678-47E4-BC62-690B17052E76}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@ -21,6 +23,10 @@ Global
{A7321FF5-0046-4C28-BFE6-DE87E6C8E60C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A7321FF5-0046-4C28-BFE6-DE87E6C8E60C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A7321FF5-0046-4C28-BFE6-DE87E6C8E60C}.Release|Any CPU.Build.0 = Release|Any CPU
{525F636D-8678-47E4-BC62-690B17052E76}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{525F636D-8678-47E4-BC62-690B17052E76}.Debug|Any CPU.Build.0 = Debug|Any CPU
{525F636D-8678-47E4-BC62-690B17052E76}.Release|Any CPU.ActiveCfg = Release|Any CPU
{525F636D-8678-47E4-BC62-690B17052E76}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE

@ -0,0 +1,62 @@
namespace ThreadSyncTest;
internal class Program
{
static void Main(string[] args)
{
int num = 0;
object obj = new object();
Thread t1 = new Thread(() =>
{
for (int i = 0; i < 1000000; i++)
{
lock (obj)
{
num++;
}
//try
//{
// Monitor.Enter(obj);
// num++; // Critical section
//}
//finally
//{
// Monitor.Exit(obj);
//}
}
});
Thread t2 = new Thread(() =>
{
for (int i = 0; i < 1000000; i++)
{
lock (obj)
{
num++;
}
//try
//{
// Monitor.Enter(obj);
// num++; // Critical section
//}
//finally
//{
// Monitor.Exit(obj);
//}
}
});
t1.Start();
t2.Start();
t1.Join();
t2.Join();
Console.WriteLine(num);
Console.ReadLine();
}
}

@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>
Loading…
Cancel
Save