<form role="search" method="get" id="searchform" class="searchform" action="https://www.teigfam.net/oyvind/home/">
				<div>
					<label class="screen-reader-text" for="s">Søk etter:</label>
					<input type="text" value="" name="s" id="s" />
					<input type="submit" id="searchsubmit" value="Søk" />
				</div>
			</form>{"id":1360,"date":"2013-04-07T20:05:25","date_gmt":"2013-04-07T18:05:25","guid":{"rendered":"http:\/\/www.teigfam.net\/oyvind\/home\/?page_id=1360"},"modified":"2025-03-09T10:47:17","modified_gmt":"2025-03-09T09:47:17","slug":"062-waiting-faster","status":"publish","type":"page","link":"https:\/\/www.teigfam.net\/oyvind\/home\/technology\/062-waiting-faster\/","title":{"rendered":"&laquo;Waiting faster&raquo;"},"content":{"rendered":"<p><br \/>\n<small>Published 7April2013, updated 23Aug2013 (09Oct2023, marking colouring. 09Mar2025 new References layout). This page is in group <a title=\"Technology\"> href=&raquo;http:\/\/www.teigfam.net\/oyvind\/home\/technology\/&raquo;&gt;Technology<\/a>.<\/small><\/p>\n<h2>First, the epilogue..<\/h2>\n<p>This blog note spurred a paper &laquo;Selective choice \u2018feathering\u2019 with XCHANs&raquo; that I had accepted for CPA-2013. It&#8217;s at <a href=\"#Ref04\">[4]<\/a>\u00a0to read, but you might perhaps read this blog note first. It&#8217;s probably a prerequisition.<\/p>\n<h2>Blog note<\/h2>\n<p>This note is a small comment to <em>Tony Hoare<\/em>&#8216;s <a href=\"#Ref01\">[1]<\/a> lecture &laquo;<span style=\"color: #ff0000;\">Concurrent programs wait faster<\/span> &raquo;\u00a0<a href=\"#Ref02\">[2]<\/a>. In the notes field of the two first pages of the ppt file I read:<\/p>\n<blockquote><p>&laquo;In the modern world, many programs spend much of their time waiting. Unfortunately, so do their users. To reduce both kinds of waiting time, it is necessary for the programs to wait faster. Wait times can be significantly reduced by waiting for two or more events concurrently \u2013 either waiting for both of them, or (more effectively) waiting just for just one of them, whichever happens first. (p1 notes)<\/p>\n<p>I will explain how this is achieved by an example, taken from real life. Most days I go to work by bus. There are two bus routes that pass between my home and my office: <span style=\"background-color: aqua;\">a city route, number 9<\/span>, and a country route, <mark>number 7<\/mark>. Let <span style=\"background-color: aqua;\">P<\/span> stand for a decision to take the more interesting city route; and let <mark>Q<\/mark> stand for the decision to take the country route, nice in good weather. If I decide <span style=\"background-color: aqua;\">P<\/span> , I find my average waiting time at the bus stop is <span style=\"background-color: aqua;\">twenty eight minutes<\/span>. If I decide <mark>Q<\/mark> , I have to wait on average <mark>the same time<\/mark>. The busses arrive independently at random, at widely varying intervals. Since <span style=\"color: #ff0000;\">the journey time is only <strong>ten minutes<\/strong><\/span>, I really have a strong motive to reduce my time spent waiting at the bus stop. There is one remarkable way that I can do this: suppose I do not decide in advance on which bus to take, but just <span style=\"color: #ff0000;\">take the first bus that arrives; then I find that my average wait time goes down to just <strong>eleven minutes<\/strong>!<\/span> I get to work much quicker, and I have done it, not by speeding up the bus, but simply by <strong><span style=\"color: #ff0000;\">waiting twice as fast, or more! <\/span><\/strong>You can tell I am waiting faster, because I\u2019m waiting less than half as long. I will now spend a little time to explain this paradox of waiting times, and show how the same technique applied inside a computer can make concurrent programs wait faster.&raquo; (p2 notes)<\/p><\/blockquote>\n<h2>But.. who&#8217;s waiting &laquo;fastest&raquo; &#8211; buffered or non-buffered comms?<\/h2>\n<p>Obviously, for an <strong>asynchronous<\/strong> system all messages that&#8217;s being waited for come from a queue. In a <strong>synchronous<\/strong> system there is no queue message queue. Will\u00a0this mean that a system built with asynchronous messages<strong> waits faster<\/strong> than a system based on synchronous communication between processes?<\/p>\n<p>It does not count on how the messages are delivered, only on how the waiting mechanism is designed. An asynchronous queue is often designed as a message pool into the waiting process, so each bus (using the same metaphor as Hoare), as it arrives to the bus stop, will leave a message that it has arrived (or: is planning to or accept a stop, really). The first in, first out (fifo) buffer makes it obvious that a waiter will get the first bus first.<\/p>\n<p>The synchronous case I am talking about has a process wait for the first event in a <span style=\"text-decoration: underline;\">selective choice.<\/span> In occam it is called <em>ALT<\/em>, in Google Go <em>select<\/em>, <em>case<\/em> and in Ada <em>select<\/em>. Unix select is not really the same, but for this discussion it could be.<\/p>\n<p>What&#8217;s waited for is a message from each of the buses. Each bus could represent a process. The selective choice is able to wait for all the buses at the same time. Whether the bus stop message arrives through a buffered channel into the choice or an unbuffered (synchronised) channel makes no difference. The choice is triggered whenever the first message arrives. Just like for the asynchronous case.<\/p>\n<p>So, a process in a selective choice could wait for a synchronous channel (or rendezvous), a buffered channel (both one-to-one) or a shared channel (many-to-one). The last case most closely resembles the asynchronous case I discussed above.<\/p>\n<p>Aside 1: In the most usual asynchronous case, the waiter would have to inspect all busses that arrive to the stop, also the numbers that will not take him in the right direction. There could be 10 busses stopping and it may have to discard maximum 8 messages before one of the two possible bus numbers arrived. A selective choice would often have a boolean expression before each component (in Go this is simulated with a pattern, see <a href=\"#Ref03\">[3]<\/a>). A selective choice would then be able to listen to only the set of busses that will take it in the right direction. So, during the waiting it could possibly burn fewer cycles of the machine to take the <em>first<\/em>. In some languages it is also possible to enable a sub-set of the many-set in a many-to-one channel.<\/p>\n<p>Aside 2: The messages from uninteresting bus processes may still be pending after the correct first, depending on the paradigm used. Messages from blocked sending would have to become picked up, but attempted sending with output guards available would enable the sender to flush an uninteresting passing.<\/p>\n<p>Aside 3: In some languages (like Erlang), a receiver is also able to listen for a certain contents of a message, and filter out uninteresting messages. I have not dicussed this case here. See <a href=\"https:\/\/www.erlang.org\/doc\/reference_manual\/expressions.html\">ERLANG &#8211; Expressions<\/a> (This paragraph is an update 11Sep2022)<\/p>\n<p>So, whether or not messages are passed over as buffered or not is not a matter here. Only how the waiting mechanism is implemented. If you wait for the first, and the first is guaranteed to get there first, that&#8217;s all that Tony Hoare would ask for. With none of these mechanisms, first will arrive second. (It could, if second had priority, but that&#8217;s another matter.) <strong>So, neither buffered nor non-buffered communication waits faster than the other.<\/strong><\/p>\n<h2>References<\/h2>\n<p><a name=\"Ref01\"><\/a><span style=\"color: #ff0000;\"><strong>[1]<\/strong><\/span> Hoare on Wikipedia:<a title=\"http:\/\/en.wikipedia.org\/wiki\/C.A.R._Hoare\" href=\"http:\/\/en.wikipedia.org\/wiki\/C.A.R._Hoare\">\u00a0http:\/\/en.wikipedia.org\/wiki\/C.A.R._Hoare<\/a>1<\/p>\n<p><a name=\"Ref02\"><\/a><span style=\"color: #ff0000;\"><strong>[2]<\/strong><\/span> <strong>Concurrent programs wait faster<\/strong> by <em>Tony Hoare<\/em> at <em>MSR TechFest 2003<\/em> (<em>Microsoft Research Technolgy Festival<\/em>(?)<br \/>\n* Hoare on Microsoft Research, Cambridge, UK:\u00a0<a title=\"http:\/\/research.microsoft.com\/en-us\/people\/thoare\/\" href=\"http:\/\/research.microsoft.com\/en-us\/people\/thoare\/\">here<\/a><br \/>\n* 11Sep2022: Since the original 2013 URLs are by now dead, I have been blunt enough to place <span style=\"color: #ff0000;\">my PDF from 2013<\/span>\u00a0<a href=\"https:\/\/www.teigfam.net\/oyvind\/blog_notes\/062\/2003_concurrent_programs_wait_faster_car_tony_hoare_-_concurrentprogramswaitfaster.pdf\">here<\/a>&#8230;<br \/>\n* .. and <span style=\"color: #ff0000;\">the notes <\/span><a href=\"https:\/\/www.teigfam.net\/oyvind\/blog_notes\/062\/2003_concurrent_programs_wait_faster_the_notes_car_tony_hoare_-_concurrentprogramswaitfaster.txt\">here<\/a>\u00a0(they are <em>needed<\/em>)<br \/>\n* The Power Point <span style=\"color: #ff0000;\">source<\/span> file is also available from the <em>WayBack Machine<\/em>, like\u00a0<a href=\"https:\/\/web.archive.org\/web\/20120313101852\/http:\/\/research.microsoft.com\/en-us\/people\/thoare\/concurrentprogramswaitfaster.ppt\">here<\/a><\/p>\n<p><a name=\"Ref03\"><\/a><span style=\"color: #ff0000;\"><strong>[3]<\/strong><\/span> <a title=\"Priority select in Go\" href=\"http:\/\/www.teigfam.net\/oyvind\/home\/technology\/047-priority-select-in-go\/\">Priority select in Go<\/a>\u00a0&#8211; a blog note of mine<\/p>\n<p><a name=\"Ref04\"><\/a><span style=\"color: #ff0000;\"><strong>[4]<\/strong><\/span> <a title=\"Selective choice feathering with XCHANs\" href=\"http:\/\/www.teigfam.net\/oyvind\/pub\/pub_details.html#FEATHERING\">Selective choice &#8216;feathering&#8217; with XCHANs<\/a>\u00a0will take you to paper and presentation at CPA-2013<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Published 7April2013, updated 23Aug2013 (09Oct2023, marking colouring. 09Mar2025 new References layout). This page is in group href=&raquo;http:\/\/www.teigfam.net\/oyvind\/home\/technology\/&raquo;&gt;Technology. First, the epilogue.. This blog note spurred a paper &laquo;Selective choice \u2018feathering\u2019 with XCHANs&raquo; that I had accepted for CPA-2013. It&#8217;s at [4]\u00a0to read, but you might perhaps read this blog note first. It&#8217;s probably a prerequisition. Blog [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":438,"menu_order":0,"comment_status":"open","ping_status":"closed","template":"page-templates\/full-width.php","meta":{"advanced_seo_description":"","jetpack_seo_html_title":"","jetpack_seo_noindex":false,"jetpack_seo_schema_type":"","_jetpack_hide_featured_image":false,"_jetpack_featured_image_duplicate_dismissed":0,"footnotes":""},"class_list":["post-1360","page","type-page","status-publish","hentry"],"jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/P75eUQ-lW","jetpack_likes_enabled":true,"jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/www.teigfam.net\/oyvind\/home\/wp-json\/wp\/v2\/pages\/1360","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.teigfam.net\/oyvind\/home\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.teigfam.net\/oyvind\/home\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.teigfam.net\/oyvind\/home\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.teigfam.net\/oyvind\/home\/wp-json\/wp\/v2\/comments?post=1360"}],"version-history":[{"count":2,"href":"https:\/\/www.teigfam.net\/oyvind\/home\/wp-json\/wp\/v2\/pages\/1360\/revisions"}],"predecessor-version":[{"id":34355,"href":"https:\/\/www.teigfam.net\/oyvind\/home\/wp-json\/wp\/v2\/pages\/1360\/revisions\/34355"}],"up":[{"embeddable":true,"href":"https:\/\/www.teigfam.net\/oyvind\/home\/wp-json\/wp\/v2\/pages\/438"}],"wp:attachment":[{"href":"https:\/\/www.teigfam.net\/oyvind\/home\/wp-json\/wp\/v2\/media?parent=1360"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}