<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Luis Guerrero EN &#187; Concurrent</title>
	<atom:link href="http://luisguerrero.net/en/category/concurrent/feed/" rel="self" type="application/rss+xml" />
	<link>http://luisguerrero.net/en</link>
	<description>.NET Stuff from London, Windows Phone 7, Silverlight, WPF and debugging</description>
	<lastBuildDate>Sat, 30 Apr 2011 15:44:21 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Concurrent programming and Managed Extensibilty Framework</title>
		<link>http://luisguerrero.net/en/2010/03/04/concurrent-programming-and-managed-extensibilty-framework/</link>
		<comments>http://luisguerrero.net/en/2010/03/04/concurrent-programming-and-managed-extensibilty-framework/#comments</comments>
		<pubDate>Thu, 04 Mar 2010 15:52:43 +0000</pubDate>
		<dc:creator>Guerrerotook</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[Concurrent]]></category>
		<category><![CDATA[MEF]]></category>

		<guid isPermaLink="false">http://www.luisguerrero.net/en/post.aspx?id=536a3435-37ec-4a7b-ad08-6fbbc9f886e5</guid>
		<description><![CDATA[Here they are! Last month I have been talking about concurrent programming on .NET Framework with Task Parallel Library and MEF (Managed Extensibility Framework), so here you have all the resources, the presentation in PowerPoint format and some demos. Task Parallel Lirarty demos in Visual Studio 2010 RC format There are no demos from MEF [...]]]></description>
			<content:encoded><![CDATA[<p>Here they are! Last month I have been talking about concurrent programming on .NET Framework with Task Parallel Library and MEF (Managed Extensibility Framework), so here you have all the resources, the presentation in PowerPoint format and some demos.</p>
<p style="text-align: center;"><a title="Concurrent programming and MEF" href="http://www.luisguerrero.net/downloads/Concurrent programming and MEF.pptx"><img class="aligncenter" style="margin-left: auto; margin-right: auto; border: 0px initial initial;" src="http://www.luisguerrero.net/Images/f929e70d1b50_EEB8/image_thumb.png" border="0" alt="clip_image001" /></a></p>
<p><a title="ParallelDemos" href="http://www.luisguerrero.net/downloads/ParallelDemos.zip" target="_blank">Task Parallel Lirarty demos in Visual Studio 2010 RC format</a></p>
<p>There are no demos from MEF but you can download some from <a href="http://www.codeplex.com/mef/">http://www.codeplex.com/mef/</a></p>
]]></content:encoded>
			<wfw:commentRss>http://luisguerrero.net/en/2010/03/04/concurrent-programming-and-managed-extensibilty-framework/feed/</wfw:commentRss>
		<slash:comments>1883</slash:comments>
		</item>
		<item>
		<title>Writing high performance parallel code with monitors</title>
		<link>http://luisguerrero.net/en/2009/11/29/writing-high-performance-parallel-code-with-monitors/</link>
		<comments>http://luisguerrero.net/en/2009/11/29/writing-high-performance-parallel-code-with-monitors/#comments</comments>
		<pubDate>Sun, 29 Nov 2009 23:02:09 +0000</pubDate>
		<dc:creator>Guerrerotook</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[Concurrent]]></category>

		<guid isPermaLink="false">http://www.luisguerrero.net/en/post.aspx?id=c8e10a96-8a2c-4758-9c1d-35faf7e39bf1</guid>
		<description><![CDATA[Multithread application is going to be next issue for developer, and we need to be ready for this big change. In .NET Framework 4, Microsoft introduced Task Parallel Library, a set of API that helps developer creating concurrent applications. That’s means that you don’t have to take care about all those concurrent issues anymore, its [...]]]></description>
			<content:encoded><![CDATA[<p>Multithread application is going to be next issue for developer, and we need to be ready for this big change. In .NET Framework 4, Microsoft introduced Task Parallel Library, a set of API that helps developer creating concurrent applications. That’s means that you don’t have to take care about all those concurrent issues anymore, its mean now is easier to create concurrent code. </p>
<p>Currently I’m working in an application that makes a hard use of all TLP code. What I do is creating a lot of <a href="http://msdn.microsoft.com/en-us/library/system.threading.tasks.task(VS.100).aspx" target="_blank">Task</a> objects to manage the executing of a set of rules that have to be executed for each Uri. I maintain a list with all the <a href="http://msdn.microsoft.com/en-us/library/system.threading.tasks.task(VS.100).aspx" target="_blank">Task</a> object to observer during time, how much <a href="http://msdn.microsoft.com/en-us/library/system.threading.tasks.task(VS.100).aspx" target="_blank">Task</a> are now running, and make some statistics about software speed. </p>
<p>This means I have UI, created in WPF that need to communicate with this code to get all running <a href="http://msdn.microsoft.com/en-us/library/system.threading.tasks.task(VS.100).aspx" target="_blank">Task</a> and display this information in the UI. Since I’m using a simple <a href="http://msdn.microsoft.com/en-us/library/6sh2ey19.aspx" target="_blank">List&lt;Task&gt;</a> to get a reference to all task created during my application execution, I need a way to lock and add Tasks to this list, enumerate this list and remove Tasks. But I want this lock to be shortest one and only lock to important operations like add and remove items when Task are finished.</p>
<p>So I have two important operations, create new Task and add to this list, and when the Task is finish removes this <a href="http://msdn.microsoft.com/en-us/library/system.threading.tasks.task(VS.100).aspx" target="_blank">Task</a> from the list, and for that I need to wait if the lock is acquired by other thread, because multiple Tasks can finish at the same time.</p>
<p>Creating task is done by a timer inside my application and this timer is set to run each second to check if the running Task in the system are lower that minimum Task running. If this is true is the moment create more Task and add to the list. Since this timer is executed every second I’ll need to lock my list of <a href="http://msdn.microsoft.com/en-us/library/system.threading.tasks.task(VS.100).aspx" target="_blank">Task</a>, do my work and then release the lock. I’m using the list as lock object. But this timer can wait more than one second until the lock is released, because other Task are now executing and finish work and remove items from my list, so It’s mean I’m creating a classic convoy in my application. Every time my timer method is executed and wait until the lock to release, this time is bigger that timer elapsed time, so I need a way to wait only a certain period of time.</p>
<p>What I do in this method is using the new API for <a href="http://msdn.microsoft.com/en-us/library/dd270976(VS.100).aspx" target="_blank">Monitor</a> in .NET 4 that attempts, for a number of milliseconds, to acquire an exclusive lock and atomically set a value that indicates whether the lock was taken or not. In my case I set these milliseconds in 400, because in this method I do more stuff than work with my list of Task. If the lock is bigger than 400ms the lock will not be acquired.</p>
<div style="border-bottom: gray 1px solid; border-left: gray 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: consolas, 'Courier New', courier, monospace; max-height: 200px; font-size: 8pt; overflow: auto; border-top: gray 1px solid; cursor: text; border-right: gray 1px solid; padding-top: 4px">
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, 'Courier New', courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #0000ff">bool</span> taken = <span style="color: #0000ff">false</span>;
<span style="color: #0000ff">try</span>
{
    Monitor.TryEnter(task, 400, <span style="color: #0000ff">ref</span> taken);
    <span style="color: #0000ff">if</span> (taken)
    {
        <span style="color: #0000ff">if</span> (!cancelationTokenSource.IsCancellationRequested)
        {
            var count = (from p <span style="color: #0000ff">in</span> task
                         <span style="color: #0000ff">where</span> p.Status == TaskStatus.Running || p.Status == TaskStatus.WaitingToRun
                         select p).Count();
        }
    }
}
<span style="color: #0000ff">finally</span>
{
    <span style="color: #0000ff">if</span> (taken)
    {
        Monitor.Exit(task);
    }
}</pre>
</div>
<p>As you can see in this code I’m calling <a href="http://msdn.microsoft.com/en-us/library/dd270976(VS.100).aspx" target="_blank">Monitor.TryEnter</a> inside a try/catch block, and then in the finally block if the lock was acquired I release the lock by calling <a href="http://msdn.microsoft.com/en-us/library/system.threading.monitor.exit(VS.100).aspx" target="_blank">Monitor.Exit</a>. I do in that way because if during the execution of my code an exception is throw, the call to <a href="http://msdn.microsoft.com/en-us/library/system.threading.monitor.exit(VS.100).aspx" target="_blank">Monitor.Exit</a> at the end of the code will not be executed anymore, causing that I have a lock that will never be released. </p>
<p>My other scenario is when I want to get all Task to count how much task are now running, I do the same inside get accessor of the property Task, but in this case I only wait for 250ms instead of 400ms. If the lock is acquired I copy the collection into a new list and there I can safety count how much Task are running.</p>
<div style="border-bottom: gray 1px solid; border-left: gray 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: consolas, 'Courier New', courier, monospace; max-height: 200px; font-size: 8pt; overflow: auto; border-top: gray 1px solid; cursor: text; border-right: gray 1px solid; padding-top: 4px">
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, 'Courier New', courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #0000ff">public</span> List&lt;Task&gt; Tasks
{
    get
    {
        <span style="color: #0000ff">bool</span> taken = <span style="color: #0000ff">false</span>;
        List&lt;Task&gt; list = <span style="color: #0000ff">null</span>;
        <span style="color: #0000ff">try</span>
        {
            Monitor.TryEnter(task, 250, <span style="color: #0000ff">ref</span> taken);
            <span style="color: #0000ff">if</span> (taken)
            {
                list = <span style="color: #0000ff">new</span> List&lt;Task&gt;(task.ToArray());
            }
        }
        <span style="color: #0000ff">finally</span>
        {
            <span style="color: #0000ff">if</span> (taken)
            {
                Monitor.Exit(task);
            }
        }
        <span style="color: #0000ff">return</span> list;
    }
}</pre>
</div>
<p>This solution is simple and keeps your code with the lowest contention rate giving boots to those important operations that need to wait until the lock is released. </p>
<p>I could also create a wrap over List&lt;T&gt; list using <a href="http://msdn.microsoft.com/en-us/library/system.threading.readerwriterlockslim.aspx" target="_blank">ReaderWriterLockSlim</a> to control read and write operation during lock, but <a href="http://msdn.microsoft.com/en-us/library/system.threading.readerwriterlockslim.aspx" target="_blank">ReaderWriterLockSlim</a> is focus on multiple readers and only one writer, and I my application I have multiple writers and only one reader. </p>
<p>All the best.</p>
<p>Luis.</p>
]]></content:encoded>
			<wfw:commentRss>http://luisguerrero.net/en/2009/11/29/writing-high-performance-parallel-code-with-monitors/feed/</wfw:commentRss>
		<slash:comments>2371</slash:comments>
		</item>
	</channel>
</rss>

