<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	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/"
		>
<channel>
	<title>Comments on: Technique: Semaphores</title>
	<atom:link href="http://dispatchevent.org/roger/technique-semaphores/feed/" rel="self" type="application/rss+xml" />
	<link>http://dispatchevent.org/roger/technique-semaphores/</link>
	<description>Collective thoughts on the Flash Platform, iOS, Unity, and any other technology we use.</description>
	<lastBuildDate>Wed, 09 May 2012 04:35:00 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
	<item>
		<title>By: simone pascucci</title>
		<link>http://dispatchevent.org/roger/technique-semaphores/comment-page-1/#comment-177359</link>
		<dc:creator>simone pascucci</dc:creator>
		<pubDate>Wed, 18 May 2011 21:37:52 +0000</pubDate>
		<guid isPermaLink="false">http://www.partlyhuman.com/blog/?p=160#comment-177359</guid>
		<description>Hi, I would like to ask if it&#039;s possible to include a example of usage. Let&#039;s say for example that we have two timers storing in a shared arraycollection the return values of a remote call. Before storing new elements in the array, they must do some preprocessing in order to avoid duplicate items and update old ones. The processes of storing new elements should be synchronized. I can&#039;t understand if I should set up event listeners or something like that and , in that case, is it possible that we fall into starvation?</description>
		<content:encoded><![CDATA[<p>Hi, I would like to ask if it&#8217;s possible to include a example of usage. Let&#8217;s say for example that we have two timers storing in a shared arraycollection the return values of a remote call. Before storing new elements in the array, they must do some preprocessing in order to avoid duplicate items and update old ones. The processes of storing new elements should be synchronized. I can&#8217;t understand if I should set up event listeners or something like that and , in that case, is it possible that we fall into starvation?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Roger Braunstein</title>
		<link>http://dispatchevent.org/roger/technique-semaphores/comment-page-1/#comment-177340</link>
		<dc:creator>Roger Braunstein</dc:creator>
		<pubDate>Fri, 15 Apr 2011 19:04:01 +0000</pubDate>
		<guid isPermaLink="false">http://www.partlyhuman.com/blog/?p=160#comment-177340</guid>
		<description>Hey Wilson, actually not quite! First, the while loop waits until the disk is unlocked for use... so as soon as you are done that loop it&#039;s just been unlocked; no need to unlock it. Secondly, you want to lock the disk so &lt;em&gt;nobody else&lt;/em&gt; can use it while you&#039;re doing your stuff. When the lock is on this signals that someone is using the resource, and that nobody else should step on his/her feet. Put another way, it guarantees that &lt;em&gt;one&lt;/em&gt; person has access to the resource at any time, not that &lt;em&gt;nobody&lt;/em&gt; has access.

So recap, the while loop either sees that no lock is present and skips ahead, or waits until there is no lock... then you lock the resource for your own use, do what you need, and unlock it so someone else can grab it.

Also @Kekoav, please note that the infinite while loop is just pseudocode. Since Flash is naturally single-threaded, you should never actually do that or it will hang. But you and Kyle are absolutely right that these are concepts implemented at the user level, to get some of the benefits of the thing without the real thing itself. I don&#039;t pretend that this user-level code is as dependable or useful as a lower-level or real threadable semaphore, but neither is it entirely useless!</description>
		<content:encoded><![CDATA[<p>Hey Wilson, actually not quite! First, the while loop waits until the disk is unlocked for use&#8230; so as soon as you are done that loop it&#8217;s just been unlocked; no need to unlock it. Secondly, you want to lock the disk so <em>nobody else</em> can use it while you&#8217;re doing your stuff. When the lock is on this signals that someone is using the resource, and that nobody else should step on his/her feet. Put another way, it guarantees that <em>one</em> person has access to the resource at any time, not that <em>nobody</em> has access.</p>
<p>So recap, the while loop either sees that no lock is present and skips ahead, or waits until there is no lock&#8230; then you lock the resource for your own use, do what you need, and unlock it so someone else can grab it.</p>
<p>Also @Kekoav, please note that the infinite while loop is just pseudocode. Since Flash is naturally single-threaded, you should never actually do that or it will hang. But you and Kyle are absolutely right that these are concepts implemented at the user level, to get some of the benefits of the thing without the real thing itself. I don&#8217;t pretend that this user-level code is as dependable or useful as a lower-level or real threadable semaphore, but neither is it entirely useless!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Wilson Silva</title>
		<link>http://dispatchevent.org/roger/technique-semaphores/comment-page-1/#comment-177339</link>
		<dc:creator>Wilson Silva</dc:creator>
		<pubDate>Fri, 15 Apr 2011 16:41:40 +0000</pubDate>
		<guid isPermaLink="false">http://www.partlyhuman.com/blog/?p=160#comment-177339</guid>
		<description>Shouldn&#039;t it be:

while (disk.isLocked())
{
	wait();
}
disk.unlock();
disk.write();
disk.lock();</description>
		<content:encoded><![CDATA[<p>Shouldn&#8217;t it be:</p>
<p>while (disk.isLocked())<br />
{<br />
	wait();<br />
}<br />
disk.unlock();<br />
disk.write();<br />
disk.lock();</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Synchronization Techniques for Flash &#38; AS3: Part I &#8211; The Semaphore- Touch My Blog</title>
		<link>http://dispatchevent.org/roger/technique-semaphores/comment-page-1/#comment-167353</link>
		<dc:creator>Synchronization Techniques for Flash &#38; AS3: Part I &#8211; The Semaphore- Touch My Blog</dc:creator>
		<pubDate>Thu, 14 Jan 2010 00:08:05 +0000</pubDate>
		<guid isPermaLink="false">http://www.partlyhuman.com/blog/?p=160#comment-167353</guid>
		<description>[...] are a couple AS3 semaphores out there (one, two), but as always I enjoy coming up with my own and then compare it with others. This guy does a [...]</description>
		<content:encoded><![CDATA[<p>[...] are a couple AS3 semaphores out there (one, two), but as always I enjoy coming up with my own and then compare it with others. This guy does a [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Kekoav</title>
		<link>http://dispatchevent.org/roger/technique-semaphores/comment-page-1/#comment-140508</link>
		<dc:creator>Kekoav</dc:creator>
		<pubDate>Wed, 04 Mar 2009 02:51:25 +0000</pubDate>
		<guid isPermaLink="false">http://www.partlyhuman.com/blog/?p=160#comment-140508</guid>
		<description>I think it&#039;s an interesting solution, but, coming from threaded languages, I want to do a blocking wait on a semaphore, without resorting to a spin lock(or busy wait).  I don&#039;t see how this is any different from using Events directly to notify and talk to each other.

I like events, but having a true semaphore would be very helpful to sync up the few async calls without having to mess with a slew of events.

Semaphores are much more useful than a single lock, it should be noted that the one use case focused on this article is really a Mutex, or Binary semaphore.  Semaphores really get interesting when combined to solve interesting problems, e.g. reader/writers producer/consumer, etc.

If I have a semaphore I want to wait() on it or it&#039;s really just masquerading as a semaphore and is some other animal.  I wish that AS3 could do it.</description>
		<content:encoded><![CDATA[<p>I think it&#8217;s an interesting solution, but, coming from threaded languages, I want to do a blocking wait on a semaphore, without resorting to a spin lock(or busy wait).  I don&#8217;t see how this is any different from using Events directly to notify and talk to each other.</p>
<p>I like events, but having a true semaphore would be very helpful to sync up the few async calls without having to mess with a slew of events.</p>
<p>Semaphores are much more useful than a single lock, it should be noted that the one use case focused on this article is really a Mutex, or Binary semaphore.  Semaphores really get interesting when combined to solve interesting problems, e.g. reader/writers producer/consumer, etc.</p>
<p>If I have a semaphore I want to wait() on it or it&#8217;s really just masquerading as a semaphore and is some other animal.  I wish that AS3 could do it.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Roger Braunstein</title>
		<link>http://dispatchevent.org/roger/technique-semaphores/comment-page-1/#comment-129003</link>
		<dc:creator>Roger Braunstein</dc:creator>
		<pubDate>Mon, 20 Oct 2008 18:55:24 +0000</pubDate>
		<guid isPermaLink="false">http://www.partlyhuman.com/blog/?p=160#comment-129003</guid>
		<description>@Hed,

Nope, scripts are executed synchronously without blocking. There are some methods in the API that execute asynchronously, such as the timer or network access. But in general all code is executed every frame from start to finish.</description>
		<content:encoded><![CDATA[<p>@Hed,</p>
<p>Nope, scripts are executed synchronously without blocking. There are some methods in the API that execute asynchronously, such as the timer or network access. But in general all code is executed every frame from start to finish.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Hed</title>
		<link>http://dispatchevent.org/roger/technique-semaphores/comment-page-1/#comment-128984</link>
		<dc:creator>Hed</dc:creator>
		<pubDate>Mon, 20 Oct 2008 15:13:02 +0000</pubDate>
		<guid isPermaLink="false">http://www.partlyhuman.com/blog/?p=160#comment-128984</guid>
		<description>I&#039;m Just returning to flash after some years,
Can you do a blocking method like you&#039;ve written:
wait();
and continuing to perform the sript after some event happens?</description>
		<content:encoded><![CDATA[<p>I&#8217;m Just returning to flash after some years,<br />
Can you do a blocking method like you&#8217;ve written:<br />
wait();<br />
and continuing to perform the sript after some event happens?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Mims Wright</title>
		<link>http://dispatchevent.org/roger/technique-semaphores/comment-page-1/#comment-125742</link>
		<dc:creator>Mims Wright</dc:creator>
		<pubDate>Sun, 14 Sep 2008 02:06:28 +0000</pubDate>
		<guid isPermaLink="false">http://www.partlyhuman.com/blog/?p=160#comment-125742</guid>
		<description>Roger, 

I finally got around to reading this. It looks pretty useful and my brain is already swimming with other, wackier implementations of Semaphore. 

I have some locks in KitchenSync already and i think this could beef it up. It would help the function sequences that already exist act even more like full-fledged command queues. 

Great stuff!</description>
		<content:encoded><![CDATA[<p>Roger, </p>
<p>I finally got around to reading this. It looks pretty useful and my brain is already swimming with other, wackier implementations of Semaphore. </p>
<p>I have some locks in KitchenSync already and i think this could beef it up. It would help the function sequences that already exist act even more like full-fledged command queues. </p>
<p>Great stuff!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Roger Braunstein</title>
		<link>http://dispatchevent.org/roger/technique-semaphores/comment-page-1/#comment-125736</link>
		<dc:creator>Roger Braunstein</dc:creator>
		<pubDate>Sun, 14 Sep 2008 01:22:24 +0000</pubDate>
		<guid isPermaLink="false">http://www.partlyhuman.com/blog/?p=160#comment-125736</guid>
		<description>Hey Mims,

The idea was to broadcast that the semaphore is becoming locked as it locks. This implementation allows you to lock it while it&#039;s already locked, if you choose... but it&#039;s not actually turning to the locked state unless it&#039;s coming from 0 locks. That&#039;s why i do _locks++ == 0. This is equivalent to doing

&lt;pre&gt;if (_locks == 0) { ... }
_locks++;
&lt;/pre&gt;

In other words, if you had 0 locks to begin with and you&#039;re locking it, tell everyone you&#039;re locked, but also increment the lock count after you check.</description>
		<content:encoded><![CDATA[<p>Hey Mims,</p>
<p>The idea was to broadcast that the semaphore is becoming locked as it locks. This implementation allows you to lock it while it&#8217;s already locked, if you choose&#8230; but it&#8217;s not actually turning to the locked state unless it&#8217;s coming from 0 locks. That&#8217;s why i do _locks++ == 0. This is equivalent to doing</p>
<pre>if (_locks == 0) { ... }
_locks++;
</pre>
<p>In other words, if you had 0 locks to begin with and you&#8217;re locking it, tell everyone you&#8217;re locked, but also increment the lock count after you check.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Mims Wright</title>
		<link>http://dispatchevent.org/roger/technique-semaphores/comment-page-1/#comment-125731</link>
		<dc:creator>Mims Wright</dc:creator>
		<pubDate>Sun, 14 Sep 2008 00:30:33 +0000</pubDate>
		<guid isPermaLink="false">http://www.partlyhuman.com/blog/?p=160#comment-125731</guid>
		<description>I may be wrong here but i think you might have a typo. In your basic implementation...

public function lock():void {
	if (_locks++ == 0) {
		dispatchEvent(new SemaphoreEvent(SemaphoreEvent.UNAVAILABLE));
	}
}

Shouldn&#039;t that be 
if (_locks++ &gt;= 0) ?

or if i&#039;m wrong maybe it would be more clear to say 
_locks ++
if (_locks == 1) {</description>
		<content:encoded><![CDATA[<p>I may be wrong here but i think you might have a typo. In your basic implementation&#8230;</p>
<p>public function lock():void {<br />
	if (_locks++ == 0) {<br />
		dispatchEvent(new SemaphoreEvent(SemaphoreEvent.UNAVAILABLE));<br />
	}<br />
}</p>
<p>Shouldn&#8217;t that be<br />
if (_locks++ &gt;= 0) ?</p>
<p>or if i&#8217;m wrong maybe it would be more clear to say<br />
_locks ++<br />
if (_locks == 1) {</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: F.Tamy</title>
		<link>http://dispatchevent.org/roger/technique-semaphores/comment-page-1/#comment-125548</link>
		<dc:creator>F.Tamy</dc:creator>
		<pubDate>Fri, 12 Sep 2008 15:00:20 +0000</pubDate>
		<guid isPermaLink="false">http://www.partlyhuman.com/blog/?p=160#comment-125548</guid>
		<description>Your idea is fine and cute, Roger Braunstein!
Thanks,
F.Tamy</description>
		<content:encoded><![CDATA[<p>Your idea is fine and cute, Roger Braunstein!<br />
Thanks,<br />
F.Tamy</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Roger Braunstein</title>
		<link>http://dispatchevent.org/roger/technique-semaphores/comment-page-1/#comment-118681</link>
		<dc:creator>Roger Braunstein</dc:creator>
		<pubDate>Wed, 25 Jun 2008 23:31:00 +0000</pubDate>
		<guid isPermaLink="false">http://www.partlyhuman.com/blog/?p=160#comment-118681</guid>
		<description>@Kyle, good point, why don&#039;t we call it a meta-semaphore... a metaphor? Kidding.

It&#039;s a higher-level semaphore for a higher-level language. As you rightly say, just because the AVM doesn&#039;t execute ActionScript code in parallel doesn&#039;t mean the concept is any less useful. It is an asynchronous language, and the order of code execution can sometimes be unpredictable if not parallel, for example code triggered by mouse events.

I have had a fun time experimenting with alternate ways of representing and dealing with asynchronous code in my applications, and this semaphore pattern works well together with my asynchronous token pattern as well. Keep an eye out for that article!</description>
		<content:encoded><![CDATA[<p>@Kyle, good point, why don&#8217;t we call it a meta-semaphore&#8230; a metaphor? Kidding.</p>
<p>It&#8217;s a higher-level semaphore for a higher-level language. As you rightly say, just because the AVM doesn&#8217;t execute ActionScript code in parallel doesn&#8217;t mean the concept is any less useful. It is an asynchronous language, and the order of code execution can sometimes be unpredictable if not parallel, for example code triggered by mouse events.</p>
<p>I have had a fun time experimenting with alternate ways of representing and dealing with asynchronous code in my applications, and this semaphore pattern works well together with my asynchronous token pattern as well. Keep an eye out for that article!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Kyle Simpson</title>
		<link>http://dispatchevent.org/roger/technique-semaphores/comment-page-1/#comment-118680</link>
		<dc:creator>Kyle Simpson</dc:creator>
		<pubDate>Wed, 25 Jun 2008 23:07:29 +0000</pubDate>
		<guid isPermaLink="false">http://www.partlyhuman.com/blog/?p=160#comment-118680</guid>
		<description>As a fellow CS geek... I should also point out that the above is not really a *true* semaphore, except in the sense that the script interpreter is *not* truly parallel, and thus will not execute two parts of a single command at the same time, anyway, with or without a &quot;higher&quot; level locking system like the one you present. 

True semaphores were/are really used to prevent much more low level things, such as the &quot;atomic&quot; set of multiple machine instructions during a variable assignment, from being partially interrupted by a parallel set of instructions being executed on the same registers or memory space (such as a comparison on a shared variable from a different thread right in the middle of that variable being assigned by another thread).  But, true semaphores are not something that more than a handful of low-level languages even fully implement, which means the ability to guarantee no race-conditions is difficult to achive.

The above is very helpful, especially in languages like AS and JS, which can implement semi-parallelism, where timers and blocks of code can cause the interpreter to jump around, and even be recursive/re-entrant. But actually, the concept here is more like a Semi-semaphore.  :)

Still a valuable concept to get your head around. I used a similar concept in a flash project a few years back where I had an &quot;Interval Manager&quot; which managed all my setIntervals and managed which things were allowed to fire and which things were &quot;locked&quot; from firing while other things were running, etc.</description>
		<content:encoded><![CDATA[<p>As a fellow CS geek&#8230; I should also point out that the above is not really a *true* semaphore, except in the sense that the script interpreter is *not* truly parallel, and thus will not execute two parts of a single command at the same time, anyway, with or without a &#8220;higher&#8221; level locking system like the one you present. </p>
<p>True semaphores were/are really used to prevent much more low level things, such as the &#8220;atomic&#8221; set of multiple machine instructions during a variable assignment, from being partially interrupted by a parallel set of instructions being executed on the same registers or memory space (such as a comparison on a shared variable from a different thread right in the middle of that variable being assigned by another thread).  But, true semaphores are not something that more than a handful of low-level languages even fully implement, which means the ability to guarantee no race-conditions is difficult to achive.</p>
<p>The above is very helpful, especially in languages like AS and JS, which can implement semi-parallelism, where timers and blocks of code can cause the interpreter to jump around, and even be recursive/re-entrant. But actually, the concept here is more like a Semi-semaphore.  :)</p>
<p>Still a valuable concept to get your head around. I used a similar concept in a flash project a few years back where I had an &#8220;Interval Manager&#8221; which managed all my setIntervals and managed which things were allowed to fire and which things were &#8220;locked&#8221; from firing while other things were running, etc.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: MorphX</title>
		<link>http://dispatchevent.org/roger/technique-semaphores/comment-page-1/#comment-118637</link>
		<dc:creator>MorphX</dc:creator>
		<pubDate>Tue, 24 Jun 2008 19:53:17 +0000</pubDate>
		<guid isPermaLink="false">http://www.partlyhuman.com/blog/?p=160#comment-118637</guid>
		<description>Really c00l :D</description>
		<content:encoded><![CDATA[<p>Really c00l :D</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: majman</title>
		<link>http://dispatchevent.org/roger/technique-semaphores/comment-page-1/#comment-118534</link>
		<dc:creator>majman</dc:creator>
		<pubDate>Sun, 22 Jun 2008 15:30:34 +0000</pubDate>
		<guid isPermaLink="false">http://www.partlyhuman.com/blog/?p=160#comment-118534</guid>
		<description>smaaaaaaaart!</description>
		<content:encoded><![CDATA[<p>smaaaaaaaart!</p>
]]></content:encoded>
	</item>
</channel>
</rss>

