Semaphore Slim and Async

Tuesday, August 16, 2016

Semaphore Slim and Async On this post I will focus on async waiting on Semaphore (using SemaphoreSlim). In general .NET is having 2 Semaphore implementations: - Semaphore which is the legacy implementation (but support cross process operations) - SemaphoreSlim which is lightweight version. It bring some performance improvements but it don’t support cross process operation. this post will focus on SemaphoreSlim WaitAsync API. Let start analyzing the following code snippet, think what’s wrong with it: Code Snippet private static SemaphoreSlim _gate = new SemaphoreSlim(4); static void Main(string args) {     for (int i = 0;...
no comments

Async / Await for .NET 4, Silverlight and Windows Phone

Tuesday, May 7, 2013

Async / Await for .NET 4, Silverlight and Windows Phone if you have to target .NET 4, Silverlight and Windows Phone and still want to use the async / await pattern. the BCL team provide you with a new NuGet package named Microsoft.Bcl.Async. this package was announced as stable a few week ago. so you can check it out if you're having VS 2012 but should target one of the above platforms. be aware that this package won't work with 2010.
no comments

EF 6: Async

Tuesday, March 19, 2013

EF 6: Async this post is the first in a series about what's new in EF 6. great improvements are about to come with Entity Framework 6. it is a major release and the first one since EF become an open source. each post in the series will be dedicate to a single feature. this post will focus on a new EF a-sync features. the first question that should be asked is, why do we need parallel data access? moreover why...

Parallel and The C# Memory Model

Wednesday, March 6, 2013

Parallel and The C# Memory Model Parallel programming can be tricky, both compiler and CPU's optimization can lead into a twilight zone's debugging. lets take the following code snippet snippet: Code Snippet class Program {     static void Main(string args)     {         Console.WriteLine("Start");         var u = new Util();         u.Exec();         Console.ReadKey();     } }   public class Util {     private bool _stop = true;     public void Exec()     {         Task t = Task.Run(() =>             {                 bool b = true;                 while (_stop)                 {                     b = !b;                 }                 Console.WriteLine("Complete {0}", b);             });         Thread.Sleep(30);         _stop = false;     } } ...

Will it crash my process

Monday, December 10, 2012

Will it crash my process here is a short question, it was taken from a real life bug that occurs at one of my customers. will the following code crash the process? Code Snippet List<object> items = new List<object>();   try {     Parallel.For(0, 10000000, t =>     {         items.Add("1");     });   } catch (AggregateException ex) { } it is a non thread safe code that should throw an IndexOutOfRangeException. but it wouldn't crash the process, but what about the following code?...

Rx – Open Source

Friday, November 9, 2012

Rx - Open Source Microsoft had announced the open sourcing of Rx. it is a great news for the Rx development community. Microsoft is still committed to the library's quality while accepting external contribution into the Rx code base. Open sourcing is the new Microsoft strategy and you can see other technology that is moving in that direction (for example ASP.NET). personally I will consider to move Visual Rx into an official Rx code base. central code base that is being managed by Microsoft is much better than...

Intel® VTune™ Amplifier XE

Thursday, July 5, 2012

Intel® VTune™ Amplifier XE this post will review the Intel® VTune™ Amplifier XE for parallel applications.  when performance is matter you should go thought the process of performance improvement cycle. the cycle may be a bit different but the measurement step cannot be omit. at the measurement step you may evaluate different aspect like duration, memory, contention, cache behavior, CPU and cores utilization. profilers is one of the method which you can evaluate those aspects. different profiler and profiler modes can evaluate different aspects. the Intel®...

Parallel.ForEach behavior

Monday, February 27, 2012

Parallel.ForEach behavior this post is a direct continuation for the previous post about "Real-life story: Blocking Collection". (Real-life story: Blocking Collection).ContinueWith (t => this post); or await (Real-life story: Blocking Collection); this post; my colleague Bram Veldhoen has suggest to demonstrate the behavior of the Parallel.ForEach thread's hunger in more pure fashion which doesn't include BlockingCollection<T> or any other high level Enumerable. the following code demonstrate the issue by using a slow Enumerable: Code...

Real-life story: Blocking Collection

Sunday, February 26, 2012

Real-life story: Blocking Collection this post will discuss a real-life story which uncover none trivial (yet logical) behavior which related to Parallel.ForEach and BlockingCollection<T>. I will explain why it happens and what how can we handle it right. it all start when Guy Eden from ITG has found that the following code seem to leak memory: Code Snippet private static void Main() {     var bc = new BlockingCollection<int>();     Task.Factory.StartNew(() =>         Parallel.ForEach(bc.GetConsumingEnumerable(), i => { }));     Console.ReadKey(); } it was leaking even those the...

Tpl Dataflow walkthrough – Part 5

Saturday, January 28, 2012

Tpl Dataflow walkthrough - Part 5 this post is a complete walkthrough of a web crawler sample that was build purely by using Tpl Dataflow. it was built on .NET 4.5 / C# 5 (on a virtual machine using VS 11). I will analyze each part of this sample, both by discussing the Dataflow blocks and the patterns in used. the sample code is available in here (it is a VS 11 project). during the walkthrough you will see the following Tpl Dataflow blocks: TransformBlock ...