Skip to content

Commit 17f6f56

Browse files
committed
✨ feat: concurrency
1 parent a42e807 commit 17f6f56

26 files changed

+1561
-0
lines changed

AlgorithmsAndDataStructures.sln

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,22 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "StringSearching", "Algorith
9393
EndProject
9494
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "StringSearchingTests", "AlgorithmsAndDataStructures2\StringSearching\StringSearchingTests\StringSearchingTests.csproj", "{2BF9E70B-310B-4382-B855-D9E5618982C1}"
9595
EndProject
96+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Concurrent", "Concurrent", "{88715E1B-809B-4B1A-B4A1-D50E9FD56263}"
97+
EndProject
98+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Queue.Client", "AlgorithmsAndDataStructures2\Concurrent\Queue.Client\Queue.Client.csproj", "{26B5C959-8A9A-41DD-9A10-18F68FDEEA6C}"
99+
EndProject
100+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Queue.Client.Locking", "AlgorithmsAndDataStructures2\Concurrent\Queue.Client.Locking\Queue.Client.Locking.csproj", "{D2D991D1-C41B-4321-A8B2-996E1C5E9738}"
101+
EndProject
102+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Queue.Client.NonLocking", "AlgorithmsAndDataStructures2\Concurrent\Queue.Client.NoLocking\Queue.Client.NonLocking.csproj", "{D2D991D1-C41B-4321-A8B2-996E1C5E9748}"
103+
EndProject
104+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Queue.Common", "AlgorithmsAndDataStructures2\Concurrent\Queue.Common\Queue.Common.csproj", "{E6346BA0-87B3-440B-A590-54DEB9215FD1}"
105+
EndProject
106+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Queue.Locking", "AlgorithmsAndDataStructures2\Concurrent\Queue.Locking\Queue.Locking.csproj", "{6F77BBD9-28A8-419E-9275-F0E2C75DE125}"
107+
EndProject
108+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Queue.NonLocking", "AlgorithmsAndDataStructures2\Concurrent\Queue.NonLocking\Queue.NonLocking.csproj", "{47C1CC18-AE32-4533-8E37-AE19F41B0B66}"
109+
EndProject
110+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Queue.ReadWriteLock", "AlgorithmsAndDataStructures2\Concurrent\Queue.ReadWriteLock\Queue.ReadWriteLock.csproj", "{8ABD47F6-4F60-478A-8978-0B2E11F41A55}"
111+
EndProject
96112
Global
97113
GlobalSection(SolutionConfigurationPlatforms) = preSolution
98114
Debug|Any CPU = Debug|Any CPU
@@ -231,6 +247,34 @@ Global
231247
{2BF9E70B-310B-4382-B855-D9E5618982C1}.Debug|Any CPU.Build.0 = Debug|Any CPU
232248
{2BF9E70B-310B-4382-B855-D9E5618982C1}.Release|Any CPU.ActiveCfg = Release|Any CPU
233249
{2BF9E70B-310B-4382-B855-D9E5618982C1}.Release|Any CPU.Build.0 = Release|Any CPU
250+
{26B5C959-8A9A-41DD-9A10-18F68FDEEA6C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
251+
{26B5C959-8A9A-41DD-9A10-18F68FDEEA6C}.Debug|Any CPU.Build.0 = Debug|Any CPU
252+
{26B5C959-8A9A-41DD-9A10-18F68FDEEA6C}.Release|Any CPU.ActiveCfg = Release|Any CPU
253+
{26B5C959-8A9A-41DD-9A10-18F68FDEEA6C}.Release|Any CPU.Build.0 = Release|Any CPU
254+
{D2D991D1-C41B-4321-A8B2-996E1C5E9738}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
255+
{D2D991D1-C41B-4321-A8B2-996E1C5E9738}.Debug|Any CPU.Build.0 = Debug|Any CPU
256+
{D2D991D1-C41B-4321-A8B2-996E1C5E9738}.Release|Any CPU.ActiveCfg = Release|Any CPU
257+
{D2D991D1-C41B-4321-A8B2-996E1C5E9738}.Release|Any CPU.Build.0 = Release|Any CPU
258+
{D2D991D1-C41B-4321-A8B2-996E1C5E9748}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
259+
{D2D991D1-C41B-4321-A8B2-996E1C5E9748}.Debug|Any CPU.Build.0 = Debug|Any CPU
260+
{D2D991D1-C41B-4321-A8B2-996E1C5E9748}.Release|Any CPU.ActiveCfg = Release|Any CPU
261+
{D2D991D1-C41B-4321-A8B2-996E1C5E9748}.Release|Any CPU.Build.0 = Release|Any CPU
262+
{E6346BA0-87B3-440B-A590-54DEB9215FD1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
263+
{E6346BA0-87B3-440B-A590-54DEB9215FD1}.Debug|Any CPU.Build.0 = Debug|Any CPU
264+
{E6346BA0-87B3-440B-A590-54DEB9215FD1}.Release|Any CPU.ActiveCfg = Release|Any CPU
265+
{E6346BA0-87B3-440B-A590-54DEB9215FD1}.Release|Any CPU.Build.0 = Release|Any CPU
266+
{6F77BBD9-28A8-419E-9275-F0E2C75DE125}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
267+
{6F77BBD9-28A8-419E-9275-F0E2C75DE125}.Debug|Any CPU.Build.0 = Debug|Any CPU
268+
{6F77BBD9-28A8-419E-9275-F0E2C75DE125}.Release|Any CPU.ActiveCfg = Release|Any CPU
269+
{6F77BBD9-28A8-419E-9275-F0E2C75DE125}.Release|Any CPU.Build.0 = Release|Any CPU
270+
{47C1CC18-AE32-4533-8E37-AE19F41B0B66}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
271+
{47C1CC18-AE32-4533-8E37-AE19F41B0B66}.Debug|Any CPU.Build.0 = Debug|Any CPU
272+
{47C1CC18-AE32-4533-8E37-AE19F41B0B66}.Release|Any CPU.ActiveCfg = Release|Any CPU
273+
{47C1CC18-AE32-4533-8E37-AE19F41B0B66}.Release|Any CPU.Build.0 = Release|Any CPU
274+
{8ABD47F6-4F60-478A-8978-0B2E11F41A55}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
275+
{8ABD47F6-4F60-478A-8978-0B2E11F41A55}.Debug|Any CPU.Build.0 = Debug|Any CPU
276+
{8ABD47F6-4F60-478A-8978-0B2E11F41A55}.Release|Any CPU.ActiveCfg = Release|Any CPU
277+
{8ABD47F6-4F60-478A-8978-0B2E11F41A55}.Release|Any CPU.Build.0 = Release|Any CPU
234278
EndGlobalSection
235279
GlobalSection(NestedProjects) = preSolution
236280
{C9A2DC88-E1E4-4492-B0B9-312F02CA30CB} = {2990F3C3-BA7A-4EF2-B65B-32228C27E2F8}
@@ -271,5 +315,12 @@ Global
271315
{207AC68B-870A-4024-A08D-D153948775CF} = {F26EB79C-6FE2-429F-AC56-987B414D5035}
272316
{BC072005-3E29-4E7A-9AAB-E65ED9BCDB3C} = {F26EB79C-6FE2-429F-AC56-987B414D5035}
273317
{2BF9E70B-310B-4382-B855-D9E5618982C1} = {F26EB79C-6FE2-429F-AC56-987B414D5035}
318+
{26B5C959-8A9A-41DD-9A10-18F68FDEEA6C} = {88715E1B-809B-4B1A-B4A1-D50E9FD56263}
319+
{D2D991D1-C41B-4321-A8B2-996E1C5E9738} = {88715E1B-809B-4B1A-B4A1-D50E9FD56263}
320+
{D2D991D1-C41B-4321-A8B2-996E1C5E9748} = {88715E1B-809B-4B1A-B4A1-D50E9FD56263}
321+
{E6346BA0-87B3-440B-A590-54DEB9215FD1} = {88715E1B-809B-4B1A-B4A1-D50E9FD56263}
322+
{6F77BBD9-28A8-419E-9275-F0E2C75DE125} = {88715E1B-809B-4B1A-B4A1-D50E9FD56263}
323+
{47C1CC18-AE32-4533-8E37-AE19F41B0B66} = {88715E1B-809B-4B1A-B4A1-D50E9FD56263}
324+
{8ABD47F6-4F60-478A-8978-0B2E11F41A55} = {88715E1B-809B-4B1A-B4A1-D50E9FD56263}
274325
EndGlobalSection
275326
EndGlobal
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System.Reflection;
2+
using System.Runtime.CompilerServices;
3+
using System.Runtime.InteropServices;
4+
5+
// General Information about an assembly is controlled through the following
6+
// set of attributes. Change these attribute values to modify the information
7+
// associated with an assembly.
8+
[assembly: AssemblyTitle("Queue.Client.Locking")]
9+
[assembly: AssemblyDescription("")]
10+
[assembly: AssemblyConfiguration("")]
11+
[assembly: AssemblyCompany("Microsoft")]
12+
[assembly: AssemblyProduct("Queue.Client.Locking")]
13+
[assembly: AssemblyCopyright("Copyright © Microsoft 2011")]
14+
[assembly: AssemblyTrademark("")]
15+
[assembly: AssemblyCulture("")]
16+
17+
// Setting ComVisible to false makes the types in this assembly not visible
18+
// to COM components. If you need to access a type in this assembly from
19+
// COM, set the ComVisible attribute to true on that type.
20+
[assembly: ComVisible(false)]
21+
22+
// The following GUID is for the ID of the typelib if this project is exposed to COM
23+
[assembly: Guid("a538064a-b56c-425f-bf03-3d491b25ad33")]
24+
25+
// Version information for an assembly consists of the following four values:
26+
//
27+
// Major Version
28+
// Minor Version
29+
// Build Number
30+
// Revision
31+
//
32+
// You can specify all the values or you can default the Build and Revision Numbers
33+
// by using the '*' as shown below:
34+
// [assembly: AssemblyVersion("1.0.*")]
35+
[assembly: AssemblyVersion("1.0.0.0")]
36+
[assembly: AssemblyFileVersion("1.0.0.0")]
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
using System;
2+
using Queue.Common;
3+
using System.Threading;
4+
using System.Collections.Generic;
5+
6+
namespace Queue.Client.Locking
7+
{
8+
public class Client
9+
{
10+
static readonly object syncLock = new object();
11+
static int SharedCount = 0;
12+
13+
public static void Main()
14+
{
15+
IQueue<Job> queue = new Queue.NonLocking.PriorityQueue<Job>();
16+
17+
List<Thread> addThreads = new List<Thread>();
18+
List<Thread> runThreads = new List<Thread>();
19+
20+
for (int i = 0; i < 4; i++)
21+
{
22+
addThreads.Add(new Thread(AddItems));
23+
runThreads.Add(new Thread(ProcessJobs));
24+
}
25+
26+
addThreads.ForEach(t => t.Start(queue));
27+
addThreads.ForEach(t => t.Join());
28+
29+
runThreads.ForEach(t => t.Start(queue));
30+
runThreads.ForEach(t => t.Join());
31+
32+
Console.WriteLine($"Total Jobs: {SharedCount}");
33+
}
34+
35+
private static void AddItems(object queueParam)
36+
{
37+
IQueue<Job> queue = queueParam as IQueue<Job>;
38+
39+
// assign random priorities
40+
Random rnd = new Random();
41+
42+
for (int actionId = 0; actionId < 50000; actionId++)
43+
{
44+
lock (syncLock)
45+
{
46+
queue.Enqueue(new Job(rnd.Next()));
47+
}
48+
}
49+
}
50+
51+
private static void ProcessJobs(object queueParam)
52+
{
53+
int count = 0;
54+
IQueue<Job> queue = queueParam as IQueue<Job>;
55+
56+
while (queue.Count > 0)
57+
{
58+
Job j = null;
59+
lock (syncLock)
60+
{
61+
if (queue.Count > 0)
62+
{
63+
j = queue.Dequeue();
64+
}
65+
}
66+
67+
if (j != null)
68+
{
69+
j.Process();
70+
count++;
71+
if (count % 5000 == 0)
72+
{
73+
Console.WriteLine("{0} - Processed {1} jobs", Thread.CurrentThread.ManagedThreadId, count);
74+
}
75+
76+
Interlocked.Increment(ref SharedCount);
77+
}
78+
}
79+
80+
Console.WriteLine("{0} - Processed {1} jobs (DONE)", Thread.CurrentThread.ManagedThreadId, count);
81+
}
82+
}
83+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<PropertyGroup>
4+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
5+
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
6+
<ProductVersion>8.0.30703</ProductVersion>
7+
<SchemaVersion>2.0</SchemaVersion>
8+
<ProjectGuid>{D2D991D1-C41B-4321-A8B2-996E1C5E9738}</ProjectGuid>
9+
<OutputType>Exe</OutputType>
10+
<AppDesignerFolder>Properties</AppDesignerFolder>
11+
<RootNamespace>Queue.Client.Locking</RootNamespace>
12+
<AssemblyName>Queue.Client.Locking</AssemblyName>
13+
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
14+
<FileAlignment>512</FileAlignment>
15+
</PropertyGroup>
16+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
17+
<DebugSymbols>true</DebugSymbols>
18+
<DebugType>full</DebugType>
19+
<Optimize>false</Optimize>
20+
<OutputPath>bin\Debug\</OutputPath>
21+
<DefineConstants>DEBUG;TRACE</DefineConstants>
22+
<ErrorReport>prompt</ErrorReport>
23+
<WarningLevel>4</WarningLevel>
24+
</PropertyGroup>
25+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
26+
<DebugType>pdbonly</DebugType>
27+
<Optimize>true</Optimize>
28+
<OutputPath>bin\Release\</OutputPath>
29+
<DefineConstants>TRACE</DefineConstants>
30+
<ErrorReport>prompt</ErrorReport>
31+
<WarningLevel>4</WarningLevel>
32+
</PropertyGroup>
33+
<PropertyGroup>
34+
<StartupObject>Queue.Client.Locking.Client</StartupObject>
35+
</PropertyGroup>
36+
<ItemGroup>
37+
<Reference Include="System" />
38+
<Reference Include="System.Core" />
39+
<Reference Include="System.Xml.Linq" />
40+
<Reference Include="System.Data.DataSetExtensions" />
41+
<Reference Include="Microsoft.CSharp" />
42+
<Reference Include="System.Data" />
43+
<Reference Include="System.Xml" />
44+
</ItemGroup>
45+
<ItemGroup>
46+
<Compile Include="Queue.Client.Locking.cs" />
47+
<Compile Include="Properties\AssemblyInfo.cs" />
48+
</ItemGroup>
49+
<ItemGroup>
50+
<ProjectReference Include="..\Queue.NonLocking\Queue.NonLocking.csproj">
51+
<Project>{47C1CC18-AE32-4533-8E37-AE19F41B0B66}</Project>
52+
<Name>Queue.NonLocking</Name>
53+
</ProjectReference>
54+
<ProjectReference Include="..\Queue.Common\Queue.Common.csproj">
55+
<Project>{E6346BA0-87B3-440B-A590-54DEB9215FD1}</Project>
56+
<Name>Queue.Common</Name>
57+
</ProjectReference>
58+
</ItemGroup>
59+
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
60+
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
61+
Other similar extension points exist, see Microsoft.Common.targets.
62+
<Target Name="BeforeBuild">
63+
</Target>
64+
<Target Name="AfterBuild">
65+
</Target>
66+
-->
67+
</Project>
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System.Reflection;
2+
using System.Runtime.CompilerServices;
3+
using System.Runtime.InteropServices;
4+
5+
// General Information about an assembly is controlled through the following
6+
// set of attributes. Change these attribute values to modify the information
7+
// associated with an assembly.
8+
[assembly: AssemblyTitle("Queue.Client.Locking")]
9+
[assembly: AssemblyDescription("")]
10+
[assembly: AssemblyConfiguration("")]
11+
[assembly: AssemblyCompany("Microsoft")]
12+
[assembly: AssemblyProduct("Queue.Client.Locking")]
13+
[assembly: AssemblyCopyright("Copyright © Microsoft 2011")]
14+
[assembly: AssemblyTrademark("")]
15+
[assembly: AssemblyCulture("")]
16+
17+
// Setting ComVisible to false makes the types in this assembly not visible
18+
// to COM components. If you need to access a type in this assembly from
19+
// COM, set the ComVisible attribute to true on that type.
20+
[assembly: ComVisible(false)]
21+
22+
// The following GUID is for the ID of the typelib if this project is exposed to COM
23+
[assembly: Guid("a538064a-b56c-425f-bf03-3d491b25ad33")]
24+
25+
// Version information for an assembly consists of the following four values:
26+
//
27+
// Major Version
28+
// Minor Version
29+
// Build Number
30+
// Revision
31+
//
32+
// You can specify all the values or you can default the Build and Revision Numbers
33+
// by using the '*' as shown below:
34+
// [assembly: AssemblyVersion("1.0.*")]
35+
[assembly: AssemblyVersion("1.0.0.0")]
36+
[assembly: AssemblyFileVersion("1.0.0.0")]
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
using System;
2+
using Queue.Common;
3+
using System.Threading;
4+
using System.Collections.Generic;
5+
6+
namespace Queue.Client.NoLocking
7+
{
8+
public class Client
9+
{
10+
static int SharedCount = 0;
11+
12+
public static void Main()
13+
{
14+
IQueue<Job> queue = new Locking.PriorityQueue<Job>();
15+
16+
List<Thread> addThreads = new List<Thread>();
17+
List<Thread> runThreads = new List<Thread>();
18+
19+
for (int i = 0; i < 4; i++)
20+
{
21+
addThreads.Add(new Thread(AddItems));
22+
runThreads.Add(new Thread(ProcesJobs));
23+
}
24+
25+
addThreads.ForEach(t => t.Start(queue));
26+
addThreads.ForEach(t => t.Join());
27+
28+
runThreads.ForEach(t => t.Start(queue));
29+
runThreads.ForEach(t => t.Join());
30+
31+
Console.WriteLine($"Total Jobs: {SharedCount}");
32+
}
33+
34+
private static void AddItems(object queueParam)
35+
{
36+
IQueue<Job> queue = queueParam as IQueue<Job>;
37+
38+
// assign random priorities
39+
Random rnd = new Random();
40+
41+
for (int actionId = 0; actionId < 50000; actionId++)
42+
{
43+
queue.Enqueue(new Job(rnd.Next()));
44+
}
45+
}
46+
47+
private static void ProcesJobs(object queueParam)
48+
{
49+
int count = 0;
50+
IQueue<Job> queue = queueParam as IQueue<Job>;
51+
52+
while (queue.Count > 0)
53+
{
54+
Job j = queue.Dequeue();
55+
j.Process();
56+
count++;
57+
if (count % 5000 == 0)
58+
{
59+
Console.WriteLine("{0} - Processed {1} jobs", Thread.CurrentThread.ManagedThreadId, count);
60+
}
61+
62+
Interlocked.Increment(ref SharedCount);
63+
}
64+
65+
Console.WriteLine("{0} - Processed {1} jobs (DONE)", Thread.CurrentThread.ManagedThreadId, count);
66+
}
67+
}
68+
}

0 commit comments

Comments
 (0)