<?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>Why Threads Are A Bad Idea</title><link>http://blogs.msdn.com/b/david_leblanc/archive/2007/04/19/why-threads-are-a-bad-idea.aspx</link><description>My friend Tim Dodd found this presentation back when we worked together at ISS somewhere around '96-'97. It's by John Ousterhout, who worked at Sun Microsystems Laboratories – the deck is dated 9/28/95. We found it hilarious, because we worked with a</description><dc:language>en-US</dc:language><generator>Telligent Evolution Platform Developer Build (Build: 5.6.50428.7875)</generator><item><title>re: Why Threads Are A Bad Idea</title><link>http://blogs.msdn.com/b/david_leblanc/archive/2007/04/19/why-threads-are-a-bad-idea.aspx#2474203</link><pubDate>Tue, 08 May 2007 07:52:46 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:2474203</guid><dc:creator>Malcolm McCaffery</dc:creator><description>&lt;P&gt;Interesting presentation on the topic. I'm not an expert programmer but have been experimenting with threads in VB.NET mainly because I hate waiting ... certainly makes the programming a lot more complex... but when done properly the result can be great.&lt;/P&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=2474203" width="1" height="1"&gt;</description></item><item><title>re: Why Threads Are A Bad Idea</title><link>http://blogs.msdn.com/b/david_leblanc/archive/2007/04/19/why-threads-are-a-bad-idea.aspx#2302863</link><pubDate>Fri, 27 Apr 2007 23:14:04 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:2302863</guid><dc:creator>Jon W</dc:creator><description>&lt;P&gt;[quote]Though I do think that lesser wizards can do it with some education.[/quote]&lt;/P&gt;
&lt;P&gt;That education, in effect, turning them into greater wizards.&lt;/P&gt;
&lt;P&gt;It really is true: It's easy to slap forms on a control, and it's even relatively easy to create well-formed SQL statements. Doing systems programming, however, is a totally different ball of wax, and the two kinds of programming are very different, requiring different skill sets. Thirty years ago, the industry knew this (you had "systems programmers" and "application programmers") but these days, the distinction seems to be lost on many people.&lt;/P&gt;
&lt;P&gt;Maybe it's a case of "you don't know enough to know how little you know..."&lt;/P&gt;
&lt;P&gt;[dcl] Clearly the challenge we all face is that in a multi-core world, effectively using threads is going to be critical to getting good perf. We can't count very much on getting faster because of cycles. I'm sure that initially, people with lots of thread experience will be in high demand, but over time, we need to make ways for more average people to get good results. It's exactly the problem we have with a lot of facets of security.&lt;/P&gt;
&lt;P&gt;I also believe that while threading techniques may be a specialty today, lots of this stuff isn't rocket science (and I know rocket science - MS in AE). Though my example is a counter-point - I've been doing threads for 12 years, have a lot of experience, and can avoid most typical mistakes, but I missed this one.&lt;/P&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=2302863" width="1" height="1"&gt;</description></item><item><title>re: Why Threads Are A Bad Idea</title><link>http://blogs.msdn.com/b/david_leblanc/archive/2007/04/19/why-threads-are-a-bad-idea.aspx#2216443</link><pubDate>Sat, 21 Apr 2007 07:20:34 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:2216443</guid><dc:creator>Aaron Margosis</dc:creator><description>&lt;P&gt;It can easily happen on a single-proc system, particularly if the waiting thread is running at a higher priority than the writing thread.&lt;/P&gt;
&lt;P&gt;Multi-threaded code can be complex, but there are plenty of cases where trying to do multiple tasks simultaneoulsy within a single thread is far more complex and error-prone.&lt;/P&gt;
&lt;P&gt;[dcl] You can get into tricky situations doing things asynchronously, even with one thread. This bug didn't rear its ugly head until I started working with multiple threads. This is why all the devs on the ISS Scanner all had dual-proc systems 10 years ago - we'd find the race conditions that way. Some things were only found on the faster systems.&lt;/P&gt;
&lt;P&gt;I try not to muck with thread priorities for exactly that reason, but the OS will tweak them for you in some cases - pending IO, being foreground, etc. will all do it.&lt;/P&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=2216443" width="1" height="1"&gt;</description></item><item><title>re: Why Threads Are A Bad Idea</title><link>http://blogs.msdn.com/b/david_leblanc/archive/2007/04/19/why-threads-are-a-bad-idea.aspx#2211868</link><pubDate>Fri, 20 Apr 2007 23:57:59 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:2211868</guid><dc:creator>Foolhardy</dc:creator><description>&lt;P&gt;To me, this is a pretty obvious race condition. WriteFile, during execution connected to an IOCP, may queue a completion packet at some undefined time after the call starts. It won't queue anything if it returns an error code.&lt;/P&gt;
&lt;P&gt;In the old code, there's a race between the function returning and the packet being queued, but the code requires the function to return first so it can add the reference. There's a window where the packet can release a reference it doesn't have yet, hence the negative ref count.&lt;/P&gt;
&lt;P&gt;WriteFile can't possibly know when the next statement in your code is going to execute, so there's no way it's going to wait for it to execute before posting the completion packet. Therefore, you have to increase the ref count BEFORE the call to WriteFile, and dereference it locally IIF an immediate error is returned (i.e. no completion packet to dereference it is forthcoming). That way, the reference is guaranteed to be added before the packet can be queued and executed.&lt;/P&gt;
&lt;P&gt;m_WriteCount.Add();&lt;/P&gt;
&lt;P&gt;if(WriteFile(...) != ERROR_SUCCESS)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;m_WriteCount.Release();&lt;/P&gt;
&lt;P&gt;//else packet executor will release&lt;/P&gt;
&lt;P&gt;[dcl] Yes, and that's exactly how I fixed it. BTW, WriteFile returns BOOL, not DWORD. The thing I found interesting about this particular scenario was that the combination of multi-proc and completion ports makes the race condition a near certainty - it's an error to have the race condition at all, even if you weren't using completion ports, but with other designs, you might avoid it from Pure, Dumb, Blind Luck, where this design holds your feet to the fire.&lt;/P&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=2211868" width="1" height="1"&gt;</description></item><item><title>re: Why Threads Are A Bad Idea</title><link>http://blogs.msdn.com/b/david_leblanc/archive/2007/04/19/why-threads-are-a-bad-idea.aspx#2211822</link><pubDate>Fri, 20 Apr 2007 23:55:12 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:2211822</guid><dc:creator>Felix von Leitner</dc:creator><description>&lt;P&gt;A reference count is conceptually similar to a lock. &amp;nbsp;So yeah, you have to lock first, then manipulate the resource. &amp;nbsp;It's kind of obvious if you think about it. &amp;nbsp;The same is true for non-threaded code, too, if it handles asynchronous events.&lt;/P&gt;
&lt;P&gt;Most people have no business writing threaded code, but not just because it is more difficult to get right, also because it is more difficult to debug and more difficult to audit. &amp;nbsp;It's making everyone's life harder. &amp;nbsp;If you don't have overwhelming clear benefits from using threads, don't.&lt;/P&gt;
&lt;P&gt;[dcl] Well, yes, which is why I used InterlockedIncrement and so on. All bugs are obvious in 20-20 hindsight. The problem I see is that people do have business writing threaded code, since that's how you're going to see perf improve. The obvious problem is that it is difficult, and prone to a whole class of subtle errors and locking design issues. Though I do think that lesser wizards can do it with some education...&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=2211822" width="1" height="1"&gt;</description></item></channel></rss>