Book review: C# 2008 and 2005 Threaded Programming
A couple of weeks ago, Packt publishing asked me to review Gastón C. Hillar book “C# 2008 and 2005 Threaded Programming: Beginner’s Guide”. They sent me a copy of this book and today, I’m ready to write a review for it. But before I’ll start reviewing it, I want to apologize to the publisher and author for the impartial review.
First of all, you should understand, that this book is about how it possible (for this book author) to write four programs (with awful user interface) using different classes from System.Threading namespace to perform tasks, rather then what is multithreaded programming and how to achieve best performance by utilizing multiple CPU power. Your own programs will not run faster after reading this book, but you’ll probably know (if you did not know before) how to use BackgroundWorker, Thread, ThreadPool, AutoResetEvent and WaitHandle classes. Also, there is a small chapter about thread context switching for UI thread delegates invocation and parallel extensions.
There are some technical misconceptions and errors in this book. But it is not the major problem of it. The problem is that while reading this book I question myself whom this book aimed at? Language style is somewhere between blog chatting (better then mine) and MSDN style documentation. I admit I don’t know quite how to categorize this, the author writes in a style that is just bizarre (even more bizarre then mine in this blog
) Overall, it sounds like I’m reading a conversation between two beginner-level programmers trying to explain one each other why they are using certain coding convention in C#.
Another half of this 395 pages book is just copy-paste stuff from Visual Studio (including it default tabulations and indentations). Here one of representative examples of such copy/paste
// Disable the Start button
butStart.Enabled = false;
// Enable the Start button
butStart.Enabled = true;…
// Some very useful property, which used as private member for another public property
private int priVeryUserfulProperty;
public int VeryUserfulProperty
{
get
{
return priVeryUserfulProperty;
}
set
{
priVeryUserfulProperty = value;
}
}
Verdict: Not very exemplary introduction to some classes inside System.Threading namespace for fellow students who like to read blogs, rather then books and documentation and do not want to understand how it works under the hoods, but write something and forget it.
3- of 5 on my scale. This book is not all bad, though, but apparently suitable for very specific audience, which definitely excludes me.
March 20th, 2009 · Comments (7)
Just released: Parallel Extensions – June CTP
Today, Parallel Programming team released 2nd CTP for Parallel Extensions to .NET Framework 3.5. Major features in this CTP are declarative parallelist (LINQ-to-Object, LINQ-to-XML), parallel tasks and imperative data and, the most important (for me) – user mode work stealing scheduler. It makes very efficient use of parallel hardware. Additional information regarding this release can be found in team blog. Also, please, report bugs to Connect in order to help those guys to produce better product.
Download June CTP of Parallel Extensions to .NET 3.5 >>
June 2nd, 2008 · Comments (0)
Parallel programming? Well, it’s all about CPU affinity or how to set processor affinity in WPF
Parallel computing is very cool technology, that makes you able to leverage tasks between processors in your system. Today it’s already impossible to buy single processor computer – when you’ll buy new PC, you’ll probably get dual core CPU at least. Today we’ll speak about how to manage affinities of tasks between CPUs in your system. For this purpose we’ll create a small game, named CoreWars. So, let’s the show begin.
What we want to do? Simple game, that includes some complicated math, executed large number of times. Here the code of one Dice roll
void SetDice()
{
List<Dice> dices = new List<Dice>();
for (int i = 0; i < MaxTries; i++)
{
double x = rnd.NextDouble();
double y = rnd.NextDouble();
double g = (Math.Pow(x, 2) + Math.Pow(y, 2) – rnd.Next()) / (Math.Sqrt(2) * y + Math.Sqrt(2) * ((x – Math.Sqrt(2)) – 1));
dices.Add((Dice)((int)g & 0×3));
}
int mP = dices.Count(dice => dice == Dice.Paper);
int mR = dices.Count(dice => dice == Dice.Rock);
int mS = dices.Count(dice => dice == Dice.Scissors);int m = (mP > mR) ? mP : mR;
m = (m > mS) ? m : mS;CurrentDice = (Dice)((m & 0×3) % 0×3);
}
Don’t even try to understand what’s going on here. The result is one dice state Paper, Rock or Scissors (the old kinds game). Now we want to run this number of times and affine each thread to one of our system’s core. How to do it?
Simple way – ask what thread are you running, then use ProcessorAffinity property of the ProcessThread to your CPU id.
ProcessThread t = Process.GetCurrentProcess().Threads.OfType<ProcessThread>().Single(pt => pt.Id == AppDomain.GetCurrentThreadId());
t.ProcessorAffinity = (IntPtr)(int)cpuID;
Very good, but what’s this warning about “System.AppDomain.GetCurrentThreadId()’ is obsolete: ‘AppDomain.GetCurrentThreadId has been deprecated because it does not provide a stable Id when managed threads are running on fibers (aka lightweight threads). To get a stable identifier for a managed thread, use the ManagedThreadId property on Thread. http://go.microsoft.com/fwlink/?linkid=14202 ” ? Why this happens to me? The simple answer is Thread Processor Affinity never was most reliable thing in Windows (as well as ThreadID). But we still want to make it. What to do?
Let’s go a bit unmanaged. We’ll use SetThreadAffinityMask and GetCurrentThread WinAPI methods to achieve what we want to.
[DllImport("kernel32.dll")]
static extern IntPtr GetCurrentThread();
[DllImport("kernel32.dll")]
static extern IntPtr SetThreadAffinityMask(IntPtr hThread, IntPtr dwThreadAffinityMask);
…
SetThreadAffinityMask(GetCurrentThread(), new IntPtr(1 << (int)cpuID));
…
SetThreadAffinityMask(GetCurrentThread(), new IntPtr(0));
So far, so good. Now threads. This step is very trivial
ParameterizedThreadStart pts = new ParameterizedThreadStart(dr.ThrowDices);
Thread t = new Thread(pts);
t.SetApartmentState(ApartmentState.MTA);
t.IsBackground = true;
t.Start(i);
We need also context switch (you remember, WPF)
ThreadPool.QueueUserWorkItem(delegate
{
int w = WaitHandle.WaitAny(wcs);
Dispatcher.Invoke(DispatcherPriority.Background, (SendOrPostCallback)delegate(object o)
{
tb.Text = string.Format("The winner is CPU #{0}", o);
}, w);
});
Now, what to do with binding? To Bind or not to Bind – that is the question! My answer is to bind! Remember, you need also a small piece of CPU time to process UI events and push frames in renderring thread. Let’s roll dices.
SetThreadAffinityMask(GetCurrentThread(), new IntPtr(1 << (int)cpuID));
App.Current.Dispatcher.Invoke(DispatcherPriority.Background, (SendOrPostCallback)delegate
{
base.Clear();if (CollectionChanged != null)
CollectionChanged(this, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset, null));
}, null);for (int i = 0; i < MaxIteractions; i++)
{
DiceRoller dr = new DiceRoller();
App.Current.Dispatcher.BeginInvoke(DispatcherPriority.Background, (SendOrPostCallback)delegate
{
base.Add(dr);if (CollectionChanged != null)
CollectionChanged(this, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, dr));
}, null);
}
SetThreadAffinityMask(GetCurrentThread(), new IntPtr(0));
ResetEvent.Set();
Also do not forget to implement INotifyCollectionChanged on result collection and INotifyPropertyChanged on dice object.
After all preparations done, we can start to build basic user interface for the presentation layer. Main window
<StackPanel Name="Root">
<StackPanel Name="LayoutRoot" Orientation="Horizontal"/>
<Button Content="Start battle" Click="Button_Click"/>
<TextBlock Name="tb" TextAlignment="Center" FontSize="15" Visibility="Collapsed"/>
</StackPanel>
Templates
<DataTemplate DataType="{x:Type l:DiceRoller}" x:Key="drt">
<Rectangle Width="5" Height="5">
<Rectangle.Style>
<Style>
<Style.Triggers>
<DataTrigger Binding="{Binding Path=CurrentDice}" Value="Paper">
<Setter Property="Rectangle.Fill" Value="Red"/>
</DataTrigger>
<DataTrigger Binding="{Binding Path=CurrentDice}" Value="Rock">
<Setter Property="Rectangle.Fill" Value="Black"/>
</DataTrigger>
<DataTrigger Binding="{Binding Path=CurrentDice}" Value="Scissors">
<Setter Property="Rectangle.Fill" Value="Blue"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Rectangle.Style>
</Rectangle>
</DataTemplate>
<ItemsPanelTemplate x:Key="wpt">
<WrapPanel/>
</ItemsPanelTemplate>
We done. the only task to perform is to detect the number of CPUs in the system and put appropriate number of content controls to show the race result. Do not forget binding too.
pc = Environment.ProcessorCount;
for (int i = 0; i < pc; i++)
{
HeaderedContentControl hcc = new HeaderedContentControl();
hcc.Width = this.Width / pc;
hcc.HorizontalAlignment = HorizontalAlignment.Stretch;
hcc.Header = string.Format("CPU {0}",i);ItemsControl ic = new ItemsControl();
ic.ItemsPanel = Resources["wpt"] as ItemsPanelTemplate;
ic.ItemTemplate = Resources["drt"] as DataTemplate;
hcc.Content = ic;
LayoutRoot.Children.Add(hcc);DiceRollers dr = new DiceRollers();
this.Resources.Add(string.Format("roller{0}", i),dr);Binding b = new Binding();
b.Source = dr;
b.IsAsync = true;
b.BindsDirectlyToSource = true;
b.Mode = BindingMode.OneTime;
ic.SetBinding(ItemsControl.ItemsSourceProperty, b);
}
That’s all. Now we can race our dices and see what CPU works better. For real life scenario, this sample looks absolutely stupid, but just thing about processor affinity in Parallel Extension CTP – why not to make us able to use it? The answer is – K.I.S.S (keep it simple, stupid). And as for me, I do not like to be stupid and I want to manage affinities! I need high performance for my applications and only me can decide to which processor dispatch which task and how.
Have a nice day and be good people.
Source code for this article.
April 13th, 2008 · Comments (6)
Some new post-mix downloads
Today is download day at MSDN. There are some very interesting things published.
- New release of Windows SDK (new classes for WPF 3.5, bugs fixes, and enhanced support for Windows Server 2008, including speech, error reporting and UAC). Download it here. For more information, visit SDK blog
- Windows XP SP3 RC2 (it’s not final release), but if you want to try it (not in production environment) download it here. As well as if you are ready to harm your system. Give a try to Windows Internet Explorer 8 Beta 1 for XP (or for Vista/Home Server 2008, or Windows Server 2003 SP2).
- There are some fixes for Microsoft XNA Game Studio 2.0 (make sure, that you know what is it. If not, come to see my TechEd session). Also, you can download Games for Windows – LIVE redistributable .
- If you are lucky Zune owner, you can download PC software 2.3 for it
- If you Virtual Earth fun, add realistic 3D capabilities to use online Live Search Maps.
- If you still have no Silverlight Tools for VS2008, be sure to download it (this is not chained installer, tools only)
Enough for this morning. Warm up your download machines and start downloading.
March 10th, 2008 · Comments (11)
How to: High performance graphics in WPF
Microsoft DPE: “WPF is ever best super performance oriented technology for creating hyper multi point graphs, using parallelism and huge in-memory persistence vector scrounged math distributed calculations… And ever more with branded new Microsoft Windows Vista 7.
Client: Errr, well…. Let’s try to apply it for our VB program…
DPE: You can easily do it yourself, but it’d be better to call someone from Microsoft Consulting Services.
Client: Let it be…
MCS: Well. It’s too huge for WPF to scale… WPF uses a retained rendering system. It saves every little pixel and make you able scale and repaint very often without the composition system blocking on callbacks to your code. However, 1,000,000 retained pixels is too huge to scale…
Client: I want it scale. They promised… They told, it’ll scale. Make it to do what I want it to do!!!
MCS: Errr, well. Let it be!
This is very common dialog between DPE, MCS and clients. Sales men want it to do, what he need it to do. Client want it to do what sales men promised to do and Services men should make it to do what they both want it to do. Today we’ll speak about retained, lazy and loose models to produce large scale graphics.
First problem: multithreading
Even before we start to work, you should know, that we cannot create dependency objects in thread other, then current UI thread. We can use locks, mutexes, semaphores, however we still can not create Dependency Objects in other thread. In order to get rid of it, we’ll have to use INofityPropertyChanged implementation, instead of Dependency Objects. This means, no Dependency Properties.
So, we’ll start with following code (I’ll reuse nice code written by Luis Diego Fallas to create Mandelbrot Fractal set)
class FractsCollection : INotifyPropertyChanged
{
Second problem: rendering thread
Well, the problem is knows. There is only one UI thread. We wont it be only one, so we’ll use our own HostVisual by Dwayne Need to enhance the performance.
Third problem: Retained objects
Actually, this is not problem. This is feature. And it can be extremely useful if you want to retain layout. Let’s create a simple example: Dragon curve fractal. It has limited number of points, generated by well known final algorithm. So, we’ll create our own geometry, derived from Shape class. The fasted geometry is StreamGeometry, so let’s use it. First of all let’s create the class and save the array of points.
public class DragonShape:Shape
{
StreamGeometry dragonGeometry;
double _angle;
List<Point> Points;
Then we’ll generate the pattern
void GeneratePattern()
{
ThreadPool.QueueUserWorkItem(delegate
{
Move(5);
Turn(GetNextPoint * System.Math.PI / 180.0);
Then, by overriding DefiningGeometry property, create the fractal
protected override System.Windows.Media.Geometry DefiningGeometry
{
get {
using (StreamGeometryContext context = dragonGeometry.Open())
{
context.BeginFigure(Points[0], false, false);
context.PolyLineTo(Points, true, false);
}
return (Geometry)dragonGeometry.GetAsFrozen();}
}
Don’t forget to tell the shape, that geometry was changed
this.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Background, (SendOrPostCallback)delegate
{
this.InvalidateVisual();
}, null);
Now, run it and we’ll have very nice vector fractal generated that can be easily resized and scaled. Here the result.
This method will work fine for 1,000, even 10,000 points. But after a while you’ll experience performance degradation. What to do? The client wants 10,000,000 (!) points (and in Winforms GDI+ it works for him)
Let’s try to understand why. Because it is not retain. It’s image! So, let’s use image to make the play fair.
The fastest BitmapSource is InteropBitmap. It has an ability to update itself from the memory section. That’s exactly what we’ll use
format = PixelFormats.Bgr32;
max = format.BitsPerPixel;
uint count = (uint)(sWidth * sHeight * format.BitsPerPixel / 8);
section = CreateFileMapping(new IntPtr(-1), IntPtr.Zero, 0×04, 0, count, null);
map = MapViewOfFile(section, 0xF001F, 0, 0, count);
pixels = new byte[count];
Marshal.Copy(pixels, 0, map, (int)count);
source = System.Windows.Interop.Imaging.CreateBitmapSourceFromMemorySection(section, (int)sWidth, (int)sHeight, format, (int)(sWidth * format.BitsPerPixel / 8), 0) as InteropBitmap;
ThreadPool.QueueUserWorkItem(delegate
{
while (true)
{
Generate();
}
});
To get the source to bind to we’ll get frozen image. Call Invalidate first to reread the source.
InteropBitmap source;
public BitmapSource Source
{
get
{
source.Invalidate();
return (BitmapSource)source.GetAsFrozen();
}
}
Now, when we ready to display we can just put pixels simultaneously (by using Parallel extension and PLINQ) and tell the WPF that our count and ImageSource property updated upon each pixel.
unsafe
{
uint* pBuffer = (uint*)map;
Parallel.For(0, (int)sHeight, delegate(int yi)
{
foreach (var p in from xi in Enumerable.Range(0, (int)sWidth).AsParallel()
let mappedX = xF(xi)
let mappedY = yF(yi)
let p0 = new TranslatePoint(xF(xi), yF(yi))
let function = constructor(p0)
select new
{
x = xi,
y = yi,
xD = mappedX,
yD = mappedY,
i = apply(function, p0)
.TakeWhile(
(x, j) => j < max && x.NormSquared() < 4.0)
.Count()
})
{
pBuffer[(int)(p.x + p.y * sWidth)] = (uint)(uint)((uint)0xFF << 24) |
(uint)(p.i << 16) |
(uint)(5*p.i <<|
(uint)(15*p.i); ;
count++;
FireUpdate();
}
});
}void FireUpdate()
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(“PixelCount”));
PropertyChanged(this, new PropertyChangedEventArgs(“Source”));
}
}
We done. Now let’s see how fast it can be to generate and display 10,000,000 live pixels (it’s about 16,000×16,00×32bit image) in screen. The maximum, I was able to get with my Dell Latitude D820 was 1,200,000 x 1,200,000 pixels indexed 4 bit image (it’s about 100,000,000 points) and my memory is over
Not bad, ah? So WPF scales and DPE are right? Not quite right, but let them to do their work and we’ll be behind to come all client’s dreams (and DPE’s promises) true.
Have a nice day and be good people. Now you can use WPF for drawing big number of points.
March 2nd, 2008 · Comments (14)
Discover other tags
My tools
- .NET Framework Detector
- Duplicate images finder
- Exchange Security Policy for Windows Mobile Devices Fix
- Gas Price Windows Vista SideBar gadget
- Israel Traffic Information Windows Vista SideBar gadget
- Localization fix for SAP ES Explorer for Visual Studio
- LocTester
- RTL and LTR in Windows Live Writer
- Silverlight controls library
- Snipping tool integration plugin for WLW
- USB FM receiver library
- Vista Battery Saver
- WebCam control for WPF
- Windows Live SkyDrive attachment for Windows Live Writer
- Wireless Migrator
- WPF Virtual Keyboard




