<?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>Vincent Gable's Blog &#187; Threads</title>
	<atom:link href="http://vgable.com/blog/tag/threads/feed/" rel="self" type="application/rss+xml" />
	<link>http://vgable.com/blog</link>
	<description>my weblog.</description>
	<lastBuildDate>Tue, 29 Nov 2011 22:20:23 +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>Threading is Wrong</title>
		<link>http://vgable.com/blog/2009/10/26/threading-is-wrong/</link>
		<comments>http://vgable.com/blog/2009/10/26/threading-is-wrong/#comments</comments>
		<pubDate>Mon, 26 Oct 2009 21:22:04 +0000</pubDate>
		<dc:creator>Vincent Gable</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Quotes]]></category>
		<category><![CDATA[Usability]]></category>
		<category><![CDATA[Cognition]]></category>
		<category><![CDATA[Parallelization]]></category>
		<category><![CDATA[Software Development]]></category>
		<category><![CDATA[Threads]]></category>
		<category><![CDATA[Tim Bray]]></category>

		<guid isPermaLink="false">http://vgable.com/blog/?p=482</guid>
		<description><![CDATA[I’m taking the following as an axiom: Exposing real pre-emptive threading with shared mutable data structures to application programmers is wrong. …It gets very hard to find humans who can actually reason about threads well enough to be usefully productive. When I give talks about this stuff, I assert that threads are a recipe for [...]]]></description>
			<content:encoded><![CDATA[<blockquote><p>I’m taking the following as an axiom: <strong>Exposing real pre-emptive threading with shared mutable data structures to application programmers is <em>wrong</em></strong>. …It gets very hard to find humans who can actually reason about threads well enough to be usefully productive.</p>
<p>When I give talks about this stuff, I assert that threads are a recipe for deadlocks, race conditions, horrible non-reproducible bugs that take endless pain to find, and hard-to-diagnose performance problems. <strong>Nobody ever pushes back</strong>.</p></blockquote>
<p>&#8211;<a href="http://www.tbray.org/ongoing/When/200x/2009/09/27/Concur-dot-next">Tim Bray</a></p>
]]></content:encoded>
			<wfw:commentRss>http://vgable.com/blog/2009/10/26/threading-is-wrong/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Thread Local Storage in Cocoa</title>
		<link>http://vgable.com/blog/2008/12/29/thread-local-storage-in-cocoa/</link>
		<comments>http://vgable.com/blog/2008/12/29/thread-local-storage-in-cocoa/#comments</comments>
		<pubDate>Mon, 29 Dec 2008 14:48:11 +0000</pubDate>
		<dc:creator>Vincent Gable</dc:creator>
				<category><![CDATA[Cocoa]]></category>
		<category><![CDATA[MacOSX]]></category>
		<category><![CDATA[Objective-C]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Sample Code]]></category>
		<category><![CDATA[Multi-Core]]></category>
		<category><![CDATA[NSThread]]></category>
		<category><![CDATA[Threads]]></category>

		<guid isPermaLink="false">http://vgable.com/blog/2008/12/29/thread-local-storage-in-cocoa/</guid>
		<description><![CDATA[[[NSThread currentThread] threadDictionary] gives you an NSMutableDictionary that you can use for thread-specific storage.]]></description>
			<content:encoded><![CDATA[<p><code>[[NSThread currentThread] threadDictionary]</code> gives you an <code>NSMutableDictionary</code> that you can use for thread-specific storage.</p>
]]></content:encoded>
			<wfw:commentRss>http://vgable.com/blog/2008/12/29/thread-local-storage-in-cocoa/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Always Update the View From the Main Thread</title>
		<link>http://vgable.com/blog/2008/12/26/always-update-the-view-from-the-main-thread/</link>
		<comments>http://vgable.com/blog/2008/12/26/always-update-the-view-from-the-main-thread/#comments</comments>
		<pubDate>Sat, 27 Dec 2008 00:04:21 +0000</pubDate>
		<dc:creator>Vincent Gable</dc:creator>
				<category><![CDATA[Announcement]]></category>
		<category><![CDATA[Bug Bite]]></category>
		<category><![CDATA[Cocoa]]></category>
		<category><![CDATA[Interface Builder]]></category>
		<category><![CDATA[MacOSX]]></category>
		<category><![CDATA[Objective-C]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Quotes]]></category>
		<category><![CDATA[AppKit]]></category>
		<category><![CDATA[Multi-Core]]></category>
		<category><![CDATA[Threads]]></category>

		<guid isPermaLink="false">http://vgable.com/blog/2008/12/26/always-update-the-view-from-the-main-thread/</guid>
		<description><![CDATA[I wish I&#8217;d read this years ago: AppKit, the GUI framework, is not thread safe. In order for things to work properly, you (almost) always need to update GUI classes from the main thread &#8211;Dave Dribin (slightly edited) I&#8217;ve run into UI + threading problem before, but I&#8217;d just never seen this limitation of AppKit [...]]]></description>
			<content:encoded><![CDATA[<p>I wish I&#8217;d read this years ago:</p>
<blockquote><p> <strong>AppKit, the GUI framework, is not thread safe. In order for things to work properly, you (almost) always need to update GUI classes from the main thread</strong></p></blockquote>
<p>&#8211;<a href="http://www.dribin.org/dave/blog/archives/2008/05/22/invoke_on_main_thread/">Dave Dribin</a> (slightly edited)</p>
<p>I&#8217;ve <a href="http://vgable.com/blog/2008/05/14/nsalert-sheets-threads-inexplicable-bugs/">run into UI + threading problem before</a>, but I&#8217;d just never seen this limitation of AppKit spelled out.</p>
<p><a href="http://www.dribin.org/dave/blog/archives/2008/05/22/invoke_on_main_thread/">Dave&#8217;s article explains how to call code on the main thread</a> better then I can.</p>
]]></content:encoded>
			<wfw:commentRss>http://vgable.com/blog/2008/12/26/always-update-the-view-from-the-main-thread/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How To Multi</title>
		<link>http://vgable.com/blog/2008/12/22/how-to-multi/</link>
		<comments>http://vgable.com/blog/2008/12/22/how-to-multi/#comments</comments>
		<pubDate>Mon, 22 Dec 2008 21:54:46 +0000</pubDate>
		<dc:creator>Vincent Gable</dc:creator>
				<category><![CDATA[C++]]></category>
		<category><![CDATA[Cocoa]]></category>
		<category><![CDATA[MacOSX]]></category>
		<category><![CDATA[Objective-C]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Quotes]]></category>
		<category><![CDATA[Distributed Computing]]></category>
		<category><![CDATA[Futurism]]></category>
		<category><![CDATA[Multi-Core]]></category>
		<category><![CDATA[Optimization]]></category>
		<category><![CDATA[Parallelization]]></category>
		<category><![CDATA[Threads]]></category>

		<guid isPermaLink="false">http://vgable.com/blog/2008/12/22/how-to-multi/</guid>
		<description><![CDATA[Avoid distributed computing unless your code is going to be run by a single client with a lot of available hardware. Being able to snarf up CPU cycles from idle hardware sitting around in the user&#8217;s house sounds cool but just doesn&#8217;t pay off most of the time. Avoid GPGPU on the Mac until Snow [...]]]></description>
			<content:encoded><![CDATA[<blockquote><p> <strong>Avoid distributed computing unless your code is going to be run by a single client with a lot of available hardware.</strong> Being able to snarf up CPU cycles from idle hardware sitting around in the user&#8217;s house sounds cool but just doesn&#8217;t pay off most of the time.</p>
<p><strong>Avoid GPGPU on the Mac until Snow Leopard ships</strong> unless you have a really good application for it. OpenCL will make GPGPU a lot more practical and flexible, so trying to shoehorn your computationally expensive code into GLSL or CoreImage today just doesn&#8217;t seem worth it.</p>
<p><strong>Using multiple processes is a good idea if the subprograms are already written. &#8230; If you&#8217;re writing your code from scratch, I don&#8217;t recommend it</strong> unless you have another good reason to write subprocesses, as it&#8217;s difficult and the reward just isn&#8217;t there.</p>
<p><strong>For multithreading, concentrate on message passing and operations.</strong> Multithreading is never easy, but these help greatly to make it simpler and less error prone.</p>
<p><strong>Good OO design will also help a lot here.</strong> It&#8217;s vastly easier to multithread an app which has already been decomposed into simple objects with well-defined interfaces and loose coupling between them.</p></blockquote>
<p>&#8211;<a href="http://www.mikeash.com/?page=pyblog/friday-qa-2008-12-19.html">Mike Ash</a> (emphasis mine, line-breaks added).  <a href="http://www.mikeash.com/?page=pyblog/friday-qa-2008-12-19.html">The article has more detail and is very much worth reading</a>.</p>
<p>One point that this advice really drives home for me is that <strong>you need to focus on making good code first, and defer micro-optimizations</strong>.  If taking the time to clean up some code makes it easier to parallelize, then you <em>are</em> optimizing your code by refactoring it, even if at a micro-level you might be making some of it slower by, say, not caching something that takes O(1) time to compute.</p>
<p>Apple does not sell a Mac that&#8217;s not multi-core, and even the iPhone has a CPU <em>and</em> a GPU. There&#8217;s no question that optimization means parallelization.  And all signs point to computers getting more parallel in the future.  Any optimization that hurts parallelization is probably a mistake.</p>
]]></content:encoded>
			<wfw:commentRss>http://vgable.com/blog/2008/12/22/how-to-multi/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>NSAlert + Sheets + Threads = Inexplicable Bugs</title>
		<link>http://vgable.com/blog/2008/05/14/nsalert-sheets-threads-inexplicable-bugs/</link>
		<comments>http://vgable.com/blog/2008/05/14/nsalert-sheets-threads-inexplicable-bugs/#comments</comments>
		<pubDate>Thu, 15 May 2008 03:08:02 +0000</pubDate>
		<dc:creator>Vincent Gable</dc:creator>
				<category><![CDATA[Bug Bite]]></category>
		<category><![CDATA[Cocoa]]></category>
		<category><![CDATA[Interface Builder]]></category>
		<category><![CDATA[MacOSX]]></category>
		<category><![CDATA[Objective-C]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Multi-Core]]></category>
		<category><![CDATA[NSAlert]]></category>
		<category><![CDATA[Threads]]></category>

		<guid isPermaLink="false">http://vgable.com/blog/2008/05/14/nsalert-sheets-threads-inexplicable-bugs/</guid>
		<description><![CDATA[UPDATED 2008-12-26: in general, all AppKit code should be called on the main thread. Problem: When using an NSAlert to display a sheet in a multi-threaded application, unexpected badness can happen. I was using beginSheetModalForWindow:modalDelegate:didEndSelector:contextInfo: To display an NSAlert as a sheet. But when the sheet appeared, the window it was attached to disappeared and [...]]]></description>
			<content:encoded><![CDATA[<p>UPDATED 2008-12-26: <a href="http://vgable.com/blog/2008/12/26/always-update-the-view-from-the-main-thread/">in general, <em>all</em> AppKit code should be called on the main thread</a>.</p>
<p><strong>Problem:</strong><br />
When using an <code><a href="http://developer.apple.com/documentation/Cocoa/Reference/ApplicationKit/Classes/NSAlert_Class/index.html"><strong>NSAlert</strong></a></code> to display a <strong>sheet</strong> in a <strong>multi-threaded application</strong>, unexpected badness can happen.</p>
<p>I was using<br />
<a href="http://developer.apple.com/documentation/Cocoa/Reference/ApplicationKit/Classes/NSAlert_Class/Reference/Reference.html#//apple_ref/occ/instm/NSAlert/beginSheetModalForWindow:modalDelegate:didEndSelector:contextInfo:"><code>beginSheetModalForWindow:modalDelegate:didEndSelector:contextInfo:</code></a><br />
To display an <code><a href="http://developer.apple.com/documentation/Cocoa/Reference/ApplicationKit/Classes/NSAlert_Class/index.html">NSAlert</a></code> as a sheet.</p>
<p>But when the sheet appeared, the window it was attached to disappeared and got into some weird broken state where it would appear iff the application was not frontmost.</p>
<p>Fortunately, I remembered <a href="http://imlocation.wordpress.com/2007/10/11/nsalert-sheet-not-having-focus/">having encountered weirdness with <code>NSAlert</code> sheets before</a>.  The symptoms were different (previously the alert didn&#8217;t have focus), but the same solution still worked.</p>
<p><strong>Solution:</strong> make sure the message to display the sheet is sent by the main thread.  To do this, put the call to <code>beginSheetModalForWindow:modalDelegate:didEndSelector:contextInfo:</code> inside another method, <code>showMyAlert</code>, then use <code>performSelectorOnMainThread:withObject:waitUntilDone:</code> to make sure <code>showMyAlert</code> is called on the main thread.</p>
<p><strong>Work around</strong> use <code>runModal</code> to display the alert as a modal dialog instead of a sheet.  <code>runModal</code> Does not appear to have any problems when called from other threads.</p>
<p><del>Just like <a href="http://imlocation.wordpress.com/2007/10/11/nsalert-sheet-not-having-focus/">last time</a>:</p>
<blockquote><p>The whole incident feels funny to me.  I suspect there may be some deeper issue at work that I am not aware of.  When I have time to investigate further I shall update this post.  Unfortunately I don&#8217;t have time to look into &#8216;solved&#8217; bugs today.</p></blockquote>
<p></del></p>
<p>UPDATED 2008-12-26: <a href="http://vgable.com/blog/2008/12/26/always-update-the-view-from-the-main-thread/">in general, <em>all</em> AppKit code should be called on the main thread</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://vgable.com/blog/2008/05/14/nsalert-sheets-threads-inexplicable-bugs/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

