Skip to content

Commit

Permalink
Merge pull request #5 from cyounes/features/thread-safe
Browse files Browse the repository at this point in the history
Features/thread safe
  • Loading branch information
cyounes authored Sep 20, 2020
2 parents b283467 + b62e603 commit b55f57d
Show file tree
Hide file tree
Showing 14 changed files with 321 additions and 326 deletions.
25 changes: 25 additions & 0 deletions .github/workflows/dotnet-core-on-master.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
name: .NET Core on Features

on:
push:
branches: [ master ]
pull_request:
branches: [ master ]

jobs:
build:

runs-on: windows-latest

steps:
- uses: actions/checkout@v2
- name: Setup .NET Core
uses: actions/setup-dotnet@v1
with:
dotnet-version: 3.1.301
- name: Install dependencies
run: dotnet restore
- name: Build
run: dotnet build ObservableConcurrentQueue.sln --configuration Release --no-restore
- name: Test
run: dotnet test ObservableConcurrentQueue.sln --no-restore --verbosity normal
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# ChangeLog

## [1.1.0] 20/09/2020

### Added
- Supported Frameworks
- Added Package Image
- ChangeLog.

### Changed
- Readme file
- Target frameworks for Demo and Tests projects.
6 changes: 0 additions & 6 deletions ObservableConcurrentQueue.Demo/App.config

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,65 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{150AC54F-DF4E-4465-8194-926DE8D2F8E1}</ProjectGuid>
<TargetFrameworks>netstandard2.0;netstandard2.1;netcoreapp1.0;netcoreapp1.1;netcoreapp2.0;netcoreapp2.1;netcoreapp2.2;netcoreapp3.0;netcoreapp3.1;net45;net451;net452;net46;net461;net462;net47;net471;net472;net48</TargetFrameworks>
<OutputType>Exe</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>System.Collections.Concurrent.Demo</RootNamespace>
<AssemblyName>ObservableConcurrentQueue.Demo</AssemblyName>
<TargetFrameworkVersion>v4.7.1</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<TargetFrameworkProfile />
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<None Include="App.config" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\ObservableConcurrentQueue\ObservableConcurrentQueue.csproj">
<Project>{707AC3DE-3EB5-451B-8D3B-492D8F3B23B6}</Project>
<Name>ObservableConcurrentQueue</Name>
</ProjectReference>
<ProjectReference Include="..\ObservableConcurrentQueue\ObservableConcurrentQueue.csproj" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>

</Project>
125 changes: 90 additions & 35 deletions ObservableConcurrentQueue.Demo/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,13 @@
// Cheikh Younes
// </Author>
// --------------------------------------------------------------------------------------------------------------------
namespace System.Collections.Concurrent.Demo
{
using System.Threading;
using System.Threading.Tasks;
using System;
using System.Collections.Concurrent;
using System.Threading;
using System.Threading.Tasks;

/// <summary>
/// The program.
/// </summary>
namespace ObservableConcurrentQueue.Demo
{
public class Program
{
#region Methods
Expand All @@ -25,43 +24,99 @@ public class Program
/// <param name="args">
/// The args.
/// </param>
private static void Main(string[] args)
public static async Task Main(string[] args)
{
Console.BackgroundColor = ConsoleColor.Yellow;
Console.ForegroundColor = ConsoleColor.DarkRed;
Console.WriteLine("### TRYING ObservableConcurrentQueue without Thread Safe ###");
Console.ResetColor();
TryItWithoutThreadSafe();
Console.WriteLine("End. Press any key to continue...");
Console.ReadKey();
Console.BackgroundColor = ConsoleColor.Yellow;
Console.ForegroundColor = ConsoleColor.DarkRed;
Console.WriteLine("### TRYING ObservableConcurrentQueue using Thread Safe ###");
Console.ResetColor();
await TryItWithThreadSafeAsync();
Console.WriteLine("End. Press any key to exit...");
Console.ReadKey();
}

private static async Task TryItWithThreadSafeAsync()
{

var observableConcurrentQueue = new ObservableConcurrentQueue<int>();
observableConcurrentQueue.ContentChanged += OnObservableConcurrentQueueContentChanged;
await Task.Run(() =>

{
Console.WriteLine("Enqueue elements...");
Parallel.For(1, 20, i => { observableConcurrentQueue.Enqueue(i); });

int item;

Console.WriteLine("Peek & Dequeue 5 elements...");
Parallel.For(0, 5, i =>
{
observableConcurrentQueue.TryPeek(out item);
Thread.Sleep(300);
observableConcurrentQueue.TryDequeue(out item);
});

Thread.Sleep(300);

observableConcurrentQueue.TryPeek(out item);
Thread.Sleep(300);

Console.WriteLine("Dequeue all elements...");

Parallel.For(1, 20, i =>
{
while (observableConcurrentQueue.TryDequeue(out item))
{
// NO SLEEP, Force Concurrence
// Thread.Sleep(300);
}
});
}
);
}

private static void TryItWithoutThreadSafe()
{
var observableConcurrentQueue = new ObservableConcurrentQueue<int>();
observableConcurrentQueue.ContentChanged += OnObservableConcurrentQueueContentChanged;
var task = new Task(
() =>
{
Console.WriteLine("Enqueue elements...");
for (int i = 1; i <= 20; i++)
{
Console.WriteLine("Enqueue elements...");
for (int i = 1; i <= 20; i++)
{
observableConcurrentQueue.Enqueue(i);
Thread.Sleep(100);
}

int item;

Console.WriteLine("Peek & Dequeue 5 elements...");
for (int i = 0; i < 5; i++)
{
observableConcurrentQueue.TryPeek(out item);
Thread.Sleep(300);
observableConcurrentQueue.TryDequeue(out item);
Thread.Sleep(300);
}
observableConcurrentQueue.Enqueue(i);
Thread.Sleep(100);
}

int item;

Console.WriteLine("Peek & Dequeue 5 elements...");
for (int i = 0; i < 5; i++)
{
observableConcurrentQueue.TryPeek(out item);
Thread.Sleep(300);
observableConcurrentQueue.TryDequeue(out item);
Thread.Sleep(300);
}

observableConcurrentQueue.TryPeek(out item);
Thread.Sleep(300);

Console.WriteLine("Dequeue all elements...");
while (observableConcurrentQueue.TryDequeue(out item))
{
Thread.Sleep(300);
}
});
Console.WriteLine("Dequeue all elements...");
while (observableConcurrentQueue.TryDequeue(out item))
{
Thread.Sleep(300);
}
});
task.Start();
Console.WriteLine("End. Press any key to exit...");
Console.ReadKey(true);
}

/// <summary>
Expand All @@ -74,7 +129,7 @@ private static void Main(string[] args)
/// The args.
/// </param>
private static void OnObservableConcurrentQueueContentChanged(
object sender,
object sender,
NotifyConcurrentQueueChangedEventArgs<int> args)
{
if (args.Action == NotifyConcurrentQueueChangedAction.Enqueue)
Expand Down Expand Up @@ -107,4 +162,4 @@ private static void OnObservableConcurrentQueueContentChanged(

#endregion
}
}
}
43 changes: 0 additions & 43 deletions ObservableConcurrentQueue.Demo/Properties/AssemblyInfo.cs

This file was deleted.

Loading

0 comments on commit b55f57d

Please sign in to comment.