{"id":4213,"date":"2013-05-30T07:00:00","date_gmt":"2013-05-30T07:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/2013\/05\/30\/the-posted-message-queue-vs-the-input-queue-vs-the-message-queue\/"},"modified":"2013-05-30T07:00:00","modified_gmt":"2013-05-30T07:00:00","slug":"the-posted-message-queue-vs-the-input-queue-vs-the-message-queue","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20130530-00\/?p=4213","title":{"rendered":"The posted message queue vs the input queue vs the message queue"},"content":{"rendered":"<p><P>\nThere are multiple ways of viewing the interaction between\nposted messages and input messages.\nMSDN prefers to view posted messages and input messages as\npart of one giant pool of messages in a <I>message queue<\/I>,\nwith rules about which ones get processed first.\nI, on the other hand, prefer to think of posted messages and\ninput messages as residing in different queues that are\nprocessed in sequence.\n<\/P>\n<P>\nBy analogy, consider a business with a policy that\nloyalty program members are served ahead of\nregular customers.\n<\/P>\n<P>\nOne way of organizing this is to form a single queue,\nbut sorting them so members go to the front.\nTo call the next person in line, you just take whoever\nis at the head of the queue.\n<\/P>\n<PRE>\nAddToQueue(Customer c)\n{\n if (c is a member and the queue contains non-members) {\n  let n = the first non-member in the queue;\n  insert c in front of n;\n } else {\n  insert c at the end of the queue;\n }\n}<\/p>\n<p>GetNextCustomer()\n{\n if (there is somebody in the queue) {\n  return the first person in the queue;\n }\n \/\/ nobody is waiting\n return null;\n}\n<\/PRE>\n<P>\nThis approach works fine from a programmatic standpoint,\nbut people might not like it when they see others cutting\nin front of them.\nYou might therefore choose to create two queues,\none for members and one for non-members:\n<\/P>\n<PRE>\nAddToQueue(Customer c)\n{\n if (c is a member) {\n  insert c at end of member queue;\n } else {\n  insert c at end of non-member queue;\n}<\/p>\n<p>GetNextCustomerInLine()\n{\n if (there is somebody in the member queue) {\n  return the first person in the member queue;\n }\n if (there is somebody in the non-member queue) {\n  return the first person in the non-member queue;\n }\n \/\/ nobody is waiting\n return null;\n}\n<\/PRE>\n<P>\nNote that this second algorithm serves customers in exactly\nthe same order; the only difference is psychological.\nCustomers might resent the presence of a <I>members<\/I> line\nsince it reminds them that other people are getting\nspecial treatment.\nOr this algorithm may not be practical because\nyou don&#8217;t have room in your lobby for two lines.\nYou might choose yet another algorithm,\nwhere you select members\nat the time the customers\nare served rather than when they arrive.\nEverybody forms a single queue, but sometimes\na customer in the middle of the queue gets pulled out\nfor special treatment.\n(Maybe they wear red hats so they are easy to spot.)\n<\/P>\n<PRE>\nAddToQueue(Customer c)\n{\n insert c at end of queue;\n}<\/p>\n<p>GetNextCustomerInLine()\n{\n if (there is a member in the queue) {\n  return the first member in the queue;\n }\n \/\/ else only non-members remaining\n if (there is somebody in the queue) {\n  return the first person in the queue;\n }\n \/\/ nobody is waiting\n return null;\n}\n<\/PRE>\n<P>\n(Some banks use a variant of this algorithm\nto\n<A HREF=\"http:\/\/blogs.msdn.com\/b\/oldnewthing\/archive\/2008\/06\/16\/8602158.aspx\">\nseparate potential bank-robbers from the rest of the customers<\/A>.)\n<\/P>\n<P>\nFrom the standpoint of determining\nwho gets served in what order,\nall of these algorithms are equivalent.\nOne may be more efficient,\none may be easier to understand,\none may be easier to maintain,\none may be easier to debug,\nbut they all accomplish the same thing.\n<\/P>\n<P>\nSo go ahead and use whatever interpretation of the message queue you like.\nThey are all equivalent.\n<\/P>\n<P>\nBut the interpretation that I use is the one I presented\n<A HREF=\"https:\/\/channel9.msdn.com\/Blogs\/scobleizer\/Raymond-Chen-PDC-05-Talk-Five-Things-Every-Win32-Programmer-Needs-to-Know\">\nseveral years ago at the PDC<\/A>,\nwhere the posted message queue and input queues are\nconsidered separate queues.\nI prefer that interpretation because it makes input queue attachment\neasier to understand.\n<\/P>\n<P>\nIn the analogy above, I prefer viewing things in terms of the\nsecond algorithm, with separate queues;\nMSDN prefers viewing things in terms of the third algorithm,\nwith a single queue, but with some messages given preferential\ntreatment over others.\n<\/P>\n<P>\nBut as I&#8217;ve already noted, the outputs of the algorithms are the\nsame given the same inputs,\nso it doesn&#8217;t matter how they are implemented internally,\nsince all you can observe as a program are the inputs and outputs.\nIt&#8217;s like the\n<A HREF=\"http:\/\/en.wikipedia.org\/wiki\/Interpretations_of_quantum_mechanics\">\nmultiple interpretations of quantum mechanics<\/A>:\nThey all attempt to describe the world from a particular viewpoint,\nbut at the end of the day, the world simply <I>is<\/I>,\nand a description is just a description.\n<\/P>\n<P>\n<!-- fwdref: Posted messages are processed ahead of input messages, even if they were posted later -->\nNext time<\/A>, we&#8217;ll explore some consequences of the interaction\nbetween posted messages and input messages.\n<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>There are multiple ways of viewing the interaction between posted messages and input messages. MSDN prefers to view posted messages and input messages as part of one giant pool of messages in a message queue, with rules about which ones get processed first. I, on the other hand, prefer to think of posted messages and [&hellip;]<\/p>\n","protected":false},"author":1069,"featured_media":111744,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[25],"class_list":["post-4213","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>There are multiple ways of viewing the interaction between posted messages and input messages. MSDN prefers to view posted messages and input messages as part of one giant pool of messages in a message queue, with rules about which ones get processed first. I, on the other hand, prefer to think of posted messages and [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/4213","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/users\/1069"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/comments?post=4213"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/4213\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media\/111744"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media?parent=4213"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=4213"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=4213"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}