{"id":94755,"date":"2016-11-21T07:00:00","date_gmt":"2016-11-21T22:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/?p=94755"},"modified":"2019-03-13T10:33:58","modified_gmt":"2019-03-13T17:33:58","slug":"20161121-00","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20161121-00\/?p=94755","title":{"rendered":"Lock free many-producer\/single-consumer patterns: A work queue with task coalescing"},"content":{"rendered":"<p>Today we&#8217;re going to implement a very simple producer\/consumer pattern: There are many producers who are producing work, and a single consumer who processes the work. The work is all identical, and it is idempotent. That means that doing the work twice is the same as doing it once. <\/p>\n<p>You see this pattern when the work is something like <i>Refresh<\/i>. Refreshing once is the same as refreshing twice, as long as the single refresh is the last one. <\/p>\n<p>The na&iuml;ve way of doing this is to avoid the coalescing at all and have each refresh request notify the consumer to perform a refresh. This means, however, that if a thousand refresh requests come in rapid succession, the consumer thread will perform a thousand refreshes, nearly all of which were unnecessary. <\/p>\n<p>A less na&iuml;ve solution is to have a flag that says whether a refresh was requested. The first person to request a refresh wakes up the consumer. The second and subsequent people to request a refresh merely reminds the consumer that another refresh request arrived. When the consumer wakes up, it performs an atomic test-and-reset of the flag. If set, then it performs a refresh, and then it goes back and performs another test-and-reset of the flag to see whether a refresh request arrived while the previous one was in progress. <\/p>\n<pre>\nLONG RefreshRequested;\n\nvoid RequestRefresh()\n{\n if (!InterlockedExchange(&amp;RefreshRequested, TRUE)) {\n  \/\/ You provide the WakeConsumer() function.\n  WakeConsumer();\n }\n}\n\n\/\/ You call this function when the consumer receives the\n\/\/ signal raised by WakeConsumer().\nvoid ConsumeRefresh()\n{\n while (InterlockedExchange(&amp;RefreshRequested, FALSE)) {\n  Refresh();\n }\n}\n<\/pre>\n<p>Note that we wake the consumer only on the transition from <code>FALSE<\/code> to <code>TRUE<\/code>. (In the lingo, the consumer is <i>edge-triggered<\/i> rather than <i>level-triggered<\/i>.) This reduces traffic between the two threads, which is important if your communication channel is something like <code>Post&shy;Message<\/code>, because you don&#8217;t want to spam the message queue with 10,000 identical messages. Not only does this cause <code>Consume&shy;Refresh<\/code> to run 9,999 times more than necessary, but you run the risk of <a HREF=\"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/20090126-00\/?p=19393\">overflowing your message queue<\/a>. <\/p>\n<p>Remember, there is only one consumer, so if <code>Wake&shy;Consumer<\/code> is called while <code>Consume&shy;Refresh<\/code> is still running, the wake will not start a new consumer immediately. It will wait for the existing <code>Consume&shy;Refresh<\/code> to complete before starting a new <code>Consume&shy;Refresh<\/code>. <\/p>\n<p>Okay, that scenario was easy. We&#8217;re still warming up. More next time. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>Starting out simple.<\/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-94755","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>Starting out simple.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/94755","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=94755"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/94755\/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=94755"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=94755"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=94755"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}