{"id":3785,"date":"2021-02-22T20:14:57","date_gmt":"2021-02-22T12:14:57","guid":{"rendered":"http:\/\/blog.coolcoding.cn\/?p=3785"},"modified":"2021-02-22T20:19:39","modified_gmt":"2021-02-22T12:19:39","slug":"cvolatile%e5%85%b3%e9%94%ae%e5%ad%97","status":"publish","type":"post","link":"https:\/\/blog.coolcoding.cn\/?p=3785","title":{"rendered":"[c]volatile\u5173\u952e\u5b57"},"content":{"rendered":"\n<p>\u672c\u610f\uff1a\u6613\u53d8\u7684\uff0c\u4e0d\u7a33\u5b9a\u7684 \uff0c\u65e0\u5b9a\u6027\u7684\uff0c\u53ef\u80fd\u6025\u5267\u6ce2\u52a8\u7684<\/p>\n\n\n\n<p>\u4f5c\u7528\uff1a\u9650\u5236\u7f16\u8bd1\u5668\u5bf9\u53d8\u91cf\u7684\u5b58\u53d6\u8fdb\u884c\u4f18\u5316<\/p>\n\n\n\n<p>\u8868\u660e\u67d0\u4e2a\u53d8\u91cf\u7684\u503c\u53ef\u80fd\u5728\u5916\u90e8\u88ab\u6539\u53d8\uff0c\u56e0\u6b64\u5bf9\u8fd9\u4e9b\u53d8\u91cf\u7684\u5b58\u53d6\u4e0d\u80fd\u7f13\u5b58\u5230\u5bc4\u5b58\u5668\uff0c\u6bcf\u6b21\u4f7f\u7528\u65f6\u9700\u8981\u91cd\u65b0\u5b58\u53d6\u3002\u8be5\u5173\u952e\u5b57\u5728\u591a\u7ebf\u7a0b\u73af\u5883\u4e0b\u7ecf\u5e38\u4f7f\u7528\uff0c\u56e0\u4e3a\u5728\u7f16\u5199\u591a\u7ebf\u7a0b\u7684\u7a0b\u5e8f\u65f6\uff0c\u540c\u4e00\u4e2a\u53d8\u91cf\u53ef\u80fd\u88ab\u591a\u4e2a\u7ebf\u7a0b\u4fee\u6539\uff0c\u800c\u7a0b\u5e8f\u901a\u8fc7\u8be5\u53d8\u91cf\u540c\u6b65\u5404\u4e2a\u7ebf\u7a0b <\/p>\n\n\n\n<p>\u4e0d\u4f1a\u5728\u4e24\u4e2a\u64cd\u4f5c\u4e4b\u95f4\u628avolatile\u53d8\u91cf\u7f13\u5b58\u5728\u5bc4\u5b58\u5668\u4e2d\u3002\u5728\u591a\u4efb\u52a1\u3001\u4e2d\u65ad\u3001\u751a\u81f3setjmp\u73af\u5883\u4e0b\uff0c\u53d8\u91cf\u53ef\u80fd\u88ab\u5176\u4ed6\u7684\u7a0b\u5e8f\u6539\u53d8\uff0c\u7f16\u8bd1\u5668\u81ea\u5df1\u65e0\u6cd5\u77e5\u9053\uff0cvolatile\u5c31\u662f\u544a\u8bc9\u7f16\u8bd1\u5668\u8fd9\u79cd\u60c5\u51b5\u3002 <\/p>\n\n\n\n<p>\u4e0d\u505a\u5e38\u91cf\u5408\u5e76\u3001\u5e38\u91cf\u4f20\u64ad\u7b49\u4f18\u5316\uff0c\u6240\u4ee5\u50cf\u4e0b\u9762\u7684\u4ee3\u7801 <br>volatile int i = 1;<br>if (i&gt;0) &#8230; \/\/ if\u7684\u6761\u4ef6\u4e0d\u4f1a\u5f53\u4f5c\u771f<\/p>\n\n\n\n<p>\u5bf9volatile\u53d8\u91cf\u7684\u8bfb\u5199\u4e0d\u4f1a\u88ab\u4f18\u5316\u6389 <\/p>\n\n\n\n<!--nextpage-->\n\n\n\n<p><strong>\u7f16\u8bd1\u5668\u4f18\u5316\u4ecb\u7ecd<\/strong><\/p>\n\n\n\n<p>\u5185\u5b58\u8bbf\u95ee\u901f\u5ea6\u8fdc\u4e0d\u53caCPU\u5904\u7406\u901f\u5ea6\uff0c\u4e3a\u63d0\u9ad8\u673a\u5668\u6574\u4f53\u6027\u80fd\uff0c\u5728\u786c\u4ef6\u4e0a\u5f15\u5165\u786c\u4ef6\u9ad8\u901f\u7f13\u5b58Cache\uff0c\u52a0\u901f\u5bf9\u5185\u5b58\u7684\u8bbf\u95ee \uff1b \u5728\u73b0\u4ee3CPU\u4e2d\u6307\u4ee4\u7684\u6267\u884c\u5e76\u4e0d\u4e00\u5b9a\u4e25\u683c\u6309\u7167\u987a\u5e8f\u6267\u884c\uff0c\u6ca1\u6709\u76f8\u5173\u6027\u7684\u6307\u4ee4\u53ef\u4ee5\u4e71\u5e8f\u6267\u884c\uff0c\u4ee5\u5145\u5206\u5229\u7528CPU\u7684\u6307\u4ee4\u6d41\u6c34\u7ebf\uff0c\u63d0\u9ad8\u6267\u884c\u901f\u5ea6\u3002\u4ee5\u4e0a\u662f\u786c\u4ef6\u7ea7\u522b\u7684\u4f18\u5316\u3002\u518d\u770b\u8f6f\u4ef6\u4e00\u7ea7\u7684\u4f18\u5316\uff1a\u4e00\u79cd\u662f\u5728\u7f16\u5199\u4ee3\u7801\u65f6\u7531\u7a0b\u5e8f\u5458\u4f18\u5316\uff0c\u53e6\u4e00\u79cd\u662f\u7531\u7f16\u8bd1\u5668\u8fdb\u884c\u4f18\u5316\u3002\u7f16\u8bd1\u5668\u4f18\u5316\u5e38\u7528\u7684\u65b9\u6cd5\u6709\uff1a\u5c06\u5185\u5b58\u53d8\u91cf\u7f13\u5b58\u5230\u5bc4\u5b58\u5668\uff1b\u8c03\u6574\u6307\u4ee4\u987a\u5e8f\u5145\u5206\u5229\u7528CPU\u6307\u4ee4\u6d41\u6c34\u7ebf\uff0c\u5e38\u89c1\u7684\u662f\u91cd\u65b0\u6392\u5e8f\u8bfb\u5199\u6307\u4ee4\u3002\u5bf9\u5e38\u89c4\u5185\u5b58\u8fdb\u884c\u4f18\u5316\u7684\u65f6\u5019\uff0c\u8fd9\u4e9b\u4f18\u5316\u662f\u900f\u660e\u7684\uff0c\u800c\u4e14\u6548\u7387\u5f88\u597d\u3002\u7531\u7f16\u8bd1\u5668\u4f18\u5316\u6216\u8005\u786c\u4ef6\u91cd\u65b0\u6392\u5e8f\u5f15\u8d77\u7684\u95ee\u9898\u7684\u89e3\u51b3\u529e\u6cd5\u662f\u5728\u4ece\u786c\u4ef6\uff08\u6216\u8005\u5176\u4ed6\u5904\u7406\u5668\uff09\u7684\u89d2\u5ea6\u770b\u5fc5\u987b\u4ee5\u7279\u5b9a\u987a\u5e8f\u6267\u884c\u7684\u64cd\u4f5c\u4e4b\u95f4\u8bbe\u7f6e\u5185\u5b58\u5c4f\u969c\uff08memory barrier\uff09\uff0clinux \u63d0\u4f9b\u4e86\u4e00\u4e2a\u5b8f\u89e3\u51b3\u7f16\u8bd1\u5668\u7684\u6267\u884c\u987a\u5e8f\u95ee\u9898\u3002  \u8fd9\u4e2a\u51fd\u6570\u901a\u77e5\u7f16\u8bd1\u5668\u63d2\u5165\u4e00\u4e2a\u5185\u5b58\u5c4f\u969c\uff0c\u4f46\u5bf9\u786c\u4ef6\u65e0\u6548\uff0c\u7f16\u8bd1\u540e\u7684\u4ee3\u7801\u4f1a\u628a\u5f53\u524dCPU\u5bc4\u5b58\u5668\u4e2d\u7684\u6240\u6709\u4fee\u6539\u8fc7\u7684\u6570\u503c\u5b58\u5165\u5185\u5b58\uff0c\u9700\u8981\u8fd9\u4e9b\u6570\u636e\u7684\u65f6\u5019\u518d\u91cd\u65b0\u4ece\u5185\u5b58\u4e2d\u8bfb\u51fa\u3002 <\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u672c\u610f\uff1a\u6613\u53d8\u7684\uff0c\u4e0d\u7a33\u5b9a\u7684 \uff0c\u65e0\u5b9a\u6027\u7684\uff0c\u53ef\u80fd\u6025\u5267\u6ce2\u52a8\u7684 \u4f5c\u7528\uff1a\u9650\u5236\u7f16\u8bd1\u5668\u5bf9\u53d8\u91cf\u7684\u5b58\u53d6\u8fdb\u884c\u4f18\u5316 \u8868\u660e\u67d0\u4e2a\u53d8\u91cf\u7684\u503c\u53ef\u80fd [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"_links":{"self":[{"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=\/wp\/v2\/posts\/3785"}],"collection":[{"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3785"}],"version-history":[{"count":4,"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=\/wp\/v2\/posts\/3785\/revisions"}],"predecessor-version":[{"id":3789,"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=\/wp\/v2\/posts\/3785\/revisions\/3789"}],"wp:attachment":[{"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3785"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3785"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3785"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}