{"id":973,"date":"2014-05-16T07:00:00","date_gmt":"2014-05-16T07:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/2014\/05\/16\/an-extensible-interlocked-arithmetic-function-via-lambdas\/"},"modified":"2014-05-16T07:00:00","modified_gmt":"2014-05-16T07:00:00","slug":"an-extensible-interlocked-arithmetic-function-via-lambdas","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20140516-00\/?p=973","title":{"rendered":"An extensible interlocked arithmetic function (via lambdas)"},"content":{"rendered":"<p>\nSome time ago, I noted that\n<a HREF=\"http:\/\/blogs.msdn.com\/b\/oldnewthing\/archive\/2004\/09\/15\/229915.aspx\">\nyou can build other interlocked operations out of\n<code>Interlocked&shy;Compare&shy;Exchange<\/code><\/a>.\nHere&#8217;s an example:\n<\/p>\n<pre>\nusing System.Threading;\npublic static int InterlockedMax(ref int location, int value)\n{\n    int initialValue, newValue;\n    do\n    {\n        initialValue = location;\n        newValue = Math.Max(initialValue, value);\n    }\n    while (Interlocked.CompareExchange(ref location, newValue,\n                                       initialValue) != initialValue);\n    return initialValue;\n}\n<\/pre>\n<p>\n(There&#8217;s a corresponding C++ version, which I leave as an exercise.)\n<\/p>\n<p>\nThis function atomically updates a &#8220;highest value seen so far&#8221; variable.\nIt follows the usual pattern:\n<\/p>\n<ul>\n<li>Capture the starting value.\n<li>Do a computation based on that value.\n<li>Compare-exchange the new value in.\n<li>If the compare-exchange failed, then start over.\n<\/ul>\n<p>\n(For bonus points, add an\nearly-out if the operation should be abandoned.)\n<\/p>\n<p>\nYou can make this function extensible by use of lambdas,\nso that you can update the old value with any computation you like.\n<\/p>\n<pre>\nusing System;\nusing System.Threading;\npublic static int InterlockedCombine(ref int location,\n                                     Func&lt;int, int&gt; update)\n{\n    int initialValue, newValue;\n    do\n    {\n        initialValue = location;\n        newValue = update(initialValue);\n    }\n    while (Interlocked.CompareExchange(ref location, newValue,\n                                       initialValue) != initialValue);\n    return initialValue;\n}\npublic static int InterlockedMax(ref int location, int value)\n{\n    return InterlockedCombine(ref location,\n                              v =&gt; Math.Max(v, value));\n}\npublic static int InterlockedMultiply(ref int location, int value)\n{\n    return InterlockedCombine(ref location,\n                              v =&gt; v * value);\n}\npublic static int InterlockedIncrementWithSaturation(\n    ref int location, int maximum)\n{\n    return InterlockedCombine(ref location,\n                              v =&gt; v &lt; maximum ? v + 1 : maximum);\n}\npublic static int InterlockedCompareExchangeIfNotEqual(\n    ref int location, int newValue, int avoidValue)\n{\n    return InterlockedCombine(ref location,\n                              v =&gt; v != avoidValue ? newValue : avoidValue);\n}\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Some time ago, I noted that you can build other interlocked operations out of Interlocked&shy;Compare&shy;Exchange. Here&#8217;s an example: using System.Threading; public static int InterlockedMax(ref int location, int value) { int initialValue, newValue; do { initialValue = location; newValue = Math.Max(initialValue, value); } while (Interlocked.CompareExchange(ref location, newValue, initialValue) != initialValue); return initialValue; } (There&#8217;s a corresponding [&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-973","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>Some time ago, I noted that you can build other interlocked operations out of Interlocked&shy;Compare&shy;Exchange. Here&#8217;s an example: using System.Threading; public static int InterlockedMax(ref int location, int value) { int initialValue, newValue; do { initialValue = location; newValue = Math.Max(initialValue, value); } while (Interlocked.CompareExchange(ref location, newValue, initialValue) != initialValue); return initialValue; } (There&#8217;s a corresponding [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/973","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=973"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/973\/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=973"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=973"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=973"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}