<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Managed blocking</title><link>http://blogs.msdn.com/cbrumme/archive/2003/04/17/51361.aspx</link><description>What’s the difference between WaitHandle.WaitOne/WaitAny/WaitAll and just PInvoke’ing to WaitForSingleObject or WaitForMultipleObjects directly? Plenty. There are several reasons why we prefer you to use managed blocking through WaitHandle or similar</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>RE: Managed blocking</title><link>http://blogs.msdn.com/cbrumme/archive/2003/04/17/51361.aspx#51362</link><pubDate>Fri, 18 Apr 2003 17:39:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:51362</guid><dc:creator>Matt</dc:creator><description>Can U provide an insight into Monitor.Wait.  In one application we have, every once in a while, a call to Monitor.Wait causes 99% CPU usage

later</description></item><item><title>RE: Managed blocking</title><link>http://blogs.msdn.com/cbrumme/archive/2003/04/17/51361.aspx#51363</link><pubDate>Fri, 18 Apr 2003 18:15:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:51363</guid><dc:creator>Chris Brumme</dc:creator><description>I'm just guessing, but...

If you are running on a multi-proc machine, Monitor.Wait will attempt to spin for a while if there is contention.  First we busy-spin, then we spin using SwitchToThread so we don't consume a CPU, and then we eventually block efficiently on an OS primitive.  On a uniproc, we obviously dispense with all this spinning since it would simply take the CPU away from the thread holding the lock.

We are routinely revisiting those spin counts, since CPUs keep getting faster / the cost of bus activity changes / etc.  Unfortunately, tuning the spin counts is an inexact science, since the optimum values also depend on how long the application holds the lock, the number of arriving waiters, etc.

In a perfect world, the system would be self-tuning.  We would adjust the spinning parameters for each lock based on historical hold times, the performance profile of your machine, etc.  In reality, it's not clear whether this would be worth the effort.

Instead, we assume that none of your locks are hot.  If we are waiting for a lock, we expect to get it right now or very, very soon.  And -- unlike the operating system -- we are prepared to sacrifice fairness to get better throughput on that lock.

If you have hot locks, then we don't have the right design.  But the solution isn't for us to change our design.  The solution is for you to reduce the heat on your lock, perhaps by doing less work in the lock or by using finer grained locks to partition the work or some other standard technique.

At least, that's our design point.

Incidentally, I think you will find our lock performance is much better in the release of the CLR we just shipped.  We did a bunch of work to improve the case where your lock never experiences any contention at all!</description></item><item><title>RE: Managed blocking</title><link>http://blogs.msdn.com/cbrumme/archive/2003/04/17/51361.aspx#51364</link><pubDate>Mon, 21 Apr 2003 03:18:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:51364</guid><dc:creator>Matt</dc:creator><description>Thanks.  We actually found the problem only seemed to occur when debugging with VS .NET 2002.</description></item><item><title>re: Managed blocking</title><link>http://blogs.msdn.com/cbrumme/archive/2003/04/17/51361.aspx#66878</link><pubDate>Wed, 04 Feb 2004 01:12:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:66878</guid><dc:creator>Chad</dc:creator><description>I have been tasked with writing a multithreaded .NET app that spawns several threads and interacts with an STA COM object and processes results from that object. The threads should be STA.&lt;br&gt;&lt;br&gt;I need to spawn these threads and wait for them all to complete and then return/exit.&lt;br&gt;&lt;br&gt;What's the best way to do this? WaitHandle.WaitAll() won't work because I'm in an STA thread and all the pooled threads are STA.&lt;br&gt;&lt;br&gt;I've tried having a thread counter and using Interlocked.Inc/Decrement and then checking to see if the count was 0 and firing a manual reset event, but it doesn't seem to be working. The event fires somehow before the count is 0. I can't observe it during debugging because it's a race condition which is eliminated by the slowness of the debugger.&lt;br&gt;&lt;br&gt;Any thoughts?&lt;br&gt;Thanks,&lt;br&gt;Chad</description></item><item><title>re: Managed blocking</title><link>http://blogs.msdn.com/cbrumme/archive/2003/04/17/51361.aspx#66909</link><pubDate>Wed, 04 Feb 2004 02:10:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:66909</guid><dc:creator>Chris Brumme</dc:creator><description>What a coincidence.  Yesterday I posted a new blog that talks about pumping and apartments.  That blog contains a section that explains why you can't do WaitAll on an STA, and suggests 3 different workarounds.  In each case, the workaround has drawbacks.  But you might find one of them works for your scenario.  The full article is at &lt;a target="_new" href="http://blogs.msdn.com/cbrumme/archive/2004/02/02/66219.aspx"&gt;http://blogs.msdn.com/cbrumme/archive/2004/02/02/66219.aspx&lt;/a&gt;.&lt;br&gt;</description></item><item><title>Thread.Sleep vs. Thread.CurrentThread.Join</title><link>http://blogs.msdn.com/cbrumme/archive/2003/04/17/51361.aspx#218493</link><pubDate>Sun, 22 Aug 2004 09:27:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:218493</guid><dc:creator>xin</dc:creator><description /></item><item><title>Ist Thread.Sleep(Int32) schlecht? | hilpers</title><link>http://blogs.msdn.com/cbrumme/archive/2003/04/17/51361.aspx#9347130</link><pubDate>Tue, 20 Jan 2009 17:00:40 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9347130</guid><dc:creator>Ist Thread.Sleep(Int32) schlecht? | hilpers</dc:creator><description>&lt;p&gt;PingBack from &lt;a rel="nofollow" target="_new" href="http://www.hilpers.com/260969-ist-thread-sleep-int32-schlecht"&gt;http://www.hilpers.com/260969-ist-thread-sleep-int32-schlecht&lt;/a&gt;&lt;/p&gt;
</description></item><item><title>A question about a while (true) loop | keyongtech</title><link>http://blogs.msdn.com/cbrumme/archive/2003/04/17/51361.aspx#9361842</link><pubDate>Thu, 22 Jan 2009 05:59:54 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9361842</guid><dc:creator>A question about a while (true) loop | keyongtech</dc:creator><description>&lt;p&gt;PingBack from &lt;a rel="nofollow" target="_new" href="http://www.keyongtech.com/679169-a-question-about-a-while"&gt;http://www.keyongtech.com/679169-a-question-about-a-while&lt;/a&gt;&lt;/p&gt;
</description></item><item><title> cbrumme s WebLog Managed blocking | Insomnia Cure</title><link>http://blogs.msdn.com/cbrumme/archive/2003/04/17/51361.aspx#9742532</link><pubDate>Sat, 13 Jun 2009 09:43:36 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9742532</guid><dc:creator> cbrumme s WebLog Managed blocking | Insomnia Cure</dc:creator><description>&lt;p&gt;PingBack from &lt;a rel="nofollow" target="_new" href="http://insomniacuresite.info/story.php?id=10179"&gt;http://insomniacuresite.info/story.php?id=10179&lt;/a&gt;&lt;/p&gt;
</description></item></channel></rss>