thread sync
This commit is contained in:
@@ -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
|
||||
|
62
MultiThread/ThreadSyncTest/Program.cs
Normal file
62
MultiThread/ThreadSyncTest/Program.cs
Normal file
@@ -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();
|
||||
}
|
||||
}
|
10
MultiThread/ThreadSyncTest/ThreadSyncTest.csproj
Normal file
10
MultiThread/ThreadSyncTest/ThreadSyncTest.csproj
Normal file
@@ -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>
|
Reference in New Issue
Block a user