{"id":447,"date":"2011-01-05T14:21:28","date_gmt":"2011-01-05T21:21:28","guid":{"rendered":"http:\/\/mattfife.net\/wordpress\/?p=447"},"modified":"2012-08-12T19:18:11","modified_gmt":"2012-08-13T02:18:11","slug":"why-the-volatile-keyword-probably-isnt-necissary-in-multi-threaded-programming","status":"publish","type":"post","link":"https:\/\/mattfife.com\/?p=447","title":{"rendered":"Why the volatile keyword probably isn&#8217;t necessary in multi-threaded programming"},"content":{"rendered":"<p><a href=\"http:\/\/software.intel.com\/en-us\/blogs\/2007\/11\/30\/volatile-almost-useless-for-multi-threaded-programming\/\" target=\"_blank\">Interesting article <\/a>from the Intel guys doing TBB.<\/p>\n<p>Arch Robinson just removed almost ALL the volatile keywords from Intel Thread Building Blocks.\u00a0 Why?\u00a0 For several reasons, but mostly because he claims that overall it slows your code, probably does not actually solve the underlying ordering problems if your code needs to be portable (a REAL concern on today&#8217;s writing of games\/apps for x86, Xbox, PS3, and iPhone devices!),\u00a0 and likely isn&#8217;t doing what you think it&#8217;s doing anyway.\u00a0 Here&#8217;s a pertinent example:<\/p>\n<blockquote><p> Sometimes programmers think of volatile as turning off optimization  of volatile accesses. That&#8217;s largely true in practice. But that&#8217;s only the volatile accesses, not the non-volatile ones. Consider this  fragment:<\/p>\n<pre>\u00a0   volatile int Ready;\u00a0\n\n\u00a0   int Message[100];\n\n\u00a0   void foo( int i ) {\n\n\u00a0       Message[i\/10] = 42;\n\n\u00a0       Ready = 1;\n\n\u00a0   }<\/pre>\n<p>It&#8217;s trying to do something very reasonable in multi-threaded  programming: write a message and then send it to another thread. The  other thread will wait until Ready becomes non-zero and then read Message. Try compiling this with &#8220;gcc -O2 -S&#8221; using gcc 4.0, or icc.  Both will do the store to Ready <em>first<\/em>, so it can be overlapped with the computation of i\/10. The reordering is <em>not<\/em> a compiler bug. It&#8217;s an aggressive optimizer doing its job.<\/p>\n<p>You might think the solution is to mark all your memory references volatile. That&#8217;s just plain silly. As the earlier quotes say, it will just slow down your code. Worst yet, it might not fix the problem. Even if the compiler does not reorder the references, the hardware might.  x86 hardware will not reorder it. Neither will an Itanium(TM) processor, because Itanium compilers insert memory fences for volatile stores. That&#8217;s a clever Itanium extension. But chips like Power(TM) will reorder. What you really need for ordering are memory fences, also called memory barriers.<\/p>\n<p>So what&#8217;s the solution for multi-threaded programming? Use a library or language extension hat implements the atomic and fence semantics. When used as intended, the operations in the library will insert the right fences. Some examples:<\/p>\n<p>* POSIX threads<br \/>\n* Windows(TM) threads<br \/>\n* OpenMP<br \/>\n* TBB<\/p><\/blockquote>\n<p>So, when is volatile actually necessary?\u00a0 It turns out there are only 3 portable cases volatile is actually needed:<\/p>\n<ul>\n<li>marking a local variable in the scope of a setjmp so that the variable does not rollback after a longjmp.<\/li>\n<li>memory that is modified by an external agent or appears to be because of a screwy memory mapping<\/li>\n<li>signal handler mischief<\/li>\n<\/ul>\n<p>And now you know, and knowing is half the battle.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Interesting article from the Intel guys doing TBB. Arch Robinson just removed almost ALL the volatile keywords from Intel Thread Building Blocks.\u00a0 Why?\u00a0 For several reasons, but mostly because he claims that overall it slows your code, probably does not actually solve the underlying ordering problems if your code needs to be portable (a REAL concern on today&#8217;s writing of games\/apps for x86, Xbox, PS3, and iPhone devices!),\u00a0 and likely isn&#8217;t doing what you think it&#8217;s doing anyway.\u00a0 Here&#8217;s a&#8230;<\/p>\n<p class=\"read-more\"><a class=\"btn btn-default\" href=\"https:\/\/mattfife.com\/?p=447\"> Read More<span class=\"screen-reader-text\">  Read More<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[7],"tags":[],"class_list":["post-447","post","type-post","status-publish","format-standard","hentry","category-technicalprogramming"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p4WECr-7d","jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/mattfife.com\/index.php?rest_route=\/wp\/v2\/posts\/447","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mattfife.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mattfife.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mattfife.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/mattfife.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=447"}],"version-history":[{"count":1,"href":"https:\/\/mattfife.com\/index.php?rest_route=\/wp\/v2\/posts\/447\/revisions"}],"predecessor-version":[{"id":579,"href":"https:\/\/mattfife.com\/index.php?rest_route=\/wp\/v2\/posts\/447\/revisions\/579"}],"wp:attachment":[{"href":"https:\/\/mattfife.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=447"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mattfife.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=447"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mattfife.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=447"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}