{"id":329,"date":"2012-07-17T22:34:11","date_gmt":"2012-07-17T13:34:11","guid":{"rendered":"http:\/\/soundengine.jp\/wordpress\/tips\/?p=329"},"modified":"2014-02-06T23:42:37","modified_gmt":"2014-02-06T14:42:37","slug":"c-mutable%e3%80%81volatile%e3%81%ae%e4%bd%bf%e3%81%84%e6%96%b9%e3%83%bb%e9%80%9f%e5%ba%a6","status":"publish","type":"post","link":"https:\/\/soundengine.jp\/wordpress\/tips\/tutorial\/329\/","title":{"rendered":"C++ mutable\u3001volatile\u306e\u4f7f\u3044\u65b9\u30fb\u901f\u5ea6"},"content":{"rendered":"<h2 id=\"content_1_0\">mutable  <a class=\"anchor_super\" id=\"f526455c\" href=\"http:\/\/soundengine.jp\/backend\/?services%2Ftips%2F2007-05-24#f526455c\" title=\"f526455c\"> <\/a><\/h2>\n<p>mutable\u306f\u3001\u30af\u30e9\u30b9\u306econst\u30e1\u30f3\u30d0\u95a2\u6570(\u30e1\u30bd\u30c3\u30c9)\u306e\u4e2d\u3067\u3082\u64cd\u4f5c\u3067\u304d\u308b\u30e1\u30f3\u30d0\u5909\u6570\u3092\u4f5c\u308b\u5834\u5408\u306b\u6307\u5b9a\u3059\u308b\u3002<!--more-->\u5177\u4f53\u7684\u306b\u3069\u3046\u3044\u3046\u5834\u5408\u306b\u3053\u308c\u3092\u4f7f\u3046\u304b\u306b\u3064\u3044\u3066\u3060\u304c\u3001\u4f8b\u3048\u3070\u3053\u306e\u3088\u3046\u306bgetFuga\u306e\u547c\u3070\u308c\u305f\u56de\u6570\u3092\u3072\u305d\u304b\u306b\u4fdd\u6301\u3057\u3066\u304a\u304f\u3068\u304b\u3001<\/p>\n<pre>class Hoge\r\n{\r\n\tmutable int _counter;\r\n\t\r\n\tint getFuga() const\r\n\t{\r\n\t\t_counter++;\r\n\t\r\n\t\treturn _fuga;\r\n\t}\r\n}<\/pre>\n<p>\u3042\u308b\u66f4\u65b0\u304c\u3042\u3063\u305f\u5834\u5408\u3060\u3051\u6587\u5b57\u5217\u3092\u66f4\u65b0\u3057\u3066\u3042\u3052\u308b\u306a\u3069\u304c\u8003\u3048\u3089\u308c\u308b\u3002<\/p>\n<pre>class Hoga\r\n{\r\n\tmutable bool _update;\r\n\tstring strHege;\r\n\r\n\tstring getHege() const\r\n\t{\r\n\t\tif( _update )\r\n\t\t{\r\n\t\t\tstrHege = getHoga();\r\n\t\t\t_update = false;\r\n\t\t}\r\n\t\t\r\n\t\treturn strHege;\r\n\t}\r\n}<\/pre>\n<p>\u305d\u3082\u305d\u3082mutable\u3068\u3044\u3046\u82f1\u8a9e\u306e\u610f\u5473\u306f\u3001<a href=\"http:\/\/dictionary.goo.ne.jp\/search.php?MT=mutable&amp;kind=ej&amp;mode=0&amp;base=1&amp;row=0\" rel=\"nofollow\">goo\u8f9e\u66f8<\/a>\u3092\u53c2\u7167\u3057\u3066<\/p>\n<blockquote>\n<p class=\"quotation\">\u5909\u308a\u3084\u3059\u3044, \u79fb\u308a\u6c17\u306e<\/p>\n<\/blockquote>\n<p>\u3068\u3044\u3046\u610f\u5473\u306e\u3088\u3046\u3060\u3002\u305d\u3082\u305d\u3082\u5024\u3092\u5c11\u3057\u3067\u3082\u66f4\u65b0\u3059\u308b\u306a\u3089const\u306a\u3069\u4f7f\u3046\u306a\u3068\u3044\u3046\u8a71\u3067\u3082\u3042\u308b\u304c\u3001\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9\u306f\u5411\u4e0a\u3055\u305b\u305f\u3044\u304c\u3001\u610f\u5473\u7684\u306b\u306fconst\u306a\u3069\u3068\u3044\u3046\u5834\u5408\u306f\u3084\u306f\u308a\u3042\u308b\u3088\u3046\u3067\u305d\u3093\u306a\u5834\u5408\u306b\u306f\u4f7f\u3048\u308b\u306e\u3067\u306f\u306a\u3044\u304b\u3068\u3002<\/p>\n<div class=\"jumpmenu\">\n<\/div>\n<h2 id=\"content_1_1\">volatile  <a class=\"anchor_super\" id=\"oe7f53c2\" href=\"http:\/\/soundengine.jp\/backend\/?services%2Ftips%2F2007-05-24#oe7f53c2\" title=\"oe7f53c2\"> <\/a><\/h2>\n<p>volatile\u3092\u5909\u6570\u306b\u4ed8\u3051\u52a0\u3048\u308b\u3053\u3068\u3067\u3001\u30b3\u30f3\u30d1\u30a4\u30e9\u306e\u6700\u9069\u5316\u3092\u7121\u52b9\u306b\u3057\u30ec\u30b8\u30b9\u30bf\u3067\u306f\u306a\u304f\u5e38\u306b\u30e1\u30e2\u30ea\u306b\u30a2\u30af\u30bb\u30b9\u3057\u3066\u5909\u6570\u3092\u5229\u7528\u3059\u308b\u3002\u30de\u30eb\u30c1\u30b9\u30ec\u30c3\u30c9\u306a\u3069\u3067\u306f\u3001\u5909\u6570\u304c\u3069\u3053\u3067\u3044\u3064\u5909\u66f4\u3055\u308c\u308b\u304b\u308f\u304b\u3089\u306a\u3044\u306e\u3067\u3001\u3053\u306e\u3088\u3046\u306a\u6307\u5b9a\u304c\u5fc5\u8981\u3060\u3068\u306e\u3053\u3068\u3002<\/p>\n<p>\u305f\u3060\u3001<a href=\"http:\/\/soundengine.jp\/wordpress\/tips\/2012\/07\/17\/vc-criticalsection%e3%81%ae%e9%80%9f%e5%ba%a6\/\">CriticalSection<\/a>\u3092\u5229\u7528\u3057\u3066\u30b9\u30ec\u30c3\u30c9\u30bb\u30fc\u30d5\u306b\u3057\u3066\u304a\u3051\u3070\u304a\u305d\u3089\u304f\u3053\u306e\u3088\u3046\u306a\u3053\u3068\u306f\u5fc5\u8981\u306a\u3044\u306e\u3067\u3001\u3053\u3053\u3089\u8fba\u306f\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9\u306e\u5411\u4e0a\u306e\u305f\u3081\u5229\u7528\u3059\u308b\u3053\u3068\u3068\u306a\u308b\u3060\u308d\u3046\u3002\u3068\u3044\u3046\u3053\u3068\u3067\u6b21\u306e\u3088\u3046\u306a\u30b3\u30fc\u30c9\u3067\u5b9f\u9a13\u3057\u3066\u307f\u305f\u3002\u3042\u308b\u30b0\u30ed\u30fc\u30d0\u30eb\u5909\u6570\u306b\u5bfe\u3057\u3066volatile\u3042\u308a\u3068\u306a\u3057\u306e\u5909\u6570\u30921024 * 1024 * 1024\u56de\u3001\u56de\u3057\u3066\u305d\u308c\u3092\u4ee3\u5165\u3059\u308b\u3068\u3044\u3046\u3082\u306e\u3067\u3042\u308b\u3002<\/p>\n<pre>int a;\r\n\r\nvoid main()\r\n{ \r\n\tint n;\r\n\tvolatile int m;\r\n\r\n\tfor( n = 0; n &lt; 1024 * 1024 * 1024; n++ )\r\n\t{\r\n\t\ta = n;\r\n\t}\r\n \r\n\tfor( m = 0; m &lt; 1024 * 1024 * 1024; m++ )\r\n\t{\r\n\t\ta = m;\r\n\t}\r\n}<\/pre>\n<p>\u7d50\u679c\u306f\u3001(\u3044\u3064\u3082\u306e\u3088\u3046\u306b<a href=\"http:\/\/soundengine.jp\/wordpress\/tips\/?p=67\">QueryPerformanceCounter<\/a>\u3067\u6e2c\u5b9a\u3057\u3066)\u524d\u306e\u30eb\u30fc\u30d7\u304c0.469265\u79d2\u306b\u5bfe\u3057\u5f8c\u308d\u306e\u30eb\u30fc\u30d7\u306f3.632109\u79d2\u3060\u3063\u305f(\u30de\u30b7\u30f3\u306fIntel Core 2 Duo E6600)\u3002\u3053\u3053\u3067m\u306e\u53c2\u7167\u56de\u6570\u304c\u4f55\u56de\u304b\u3042\u308b\u306e\u3060\u304c\u3001\u901f\u5ea6\u7684\u306b\u306f7\u301c8\u500d\u306b\u306a\u3063\u3066\u3057\u307e\u3063\u3066\u3044\u308b\u3002\u9006\u30a2\u30bb\u30f3\u30d6\u30ea\u3059\u308b\u3068\u30b3\u30fc\u30c9\u3082\u5909\u308f\u3063\u3066\u304a\u308a\u3001\u524d\u306e\u30eb\u30fc\u30d7\u304c\u4e0b\u306e\u3088\u3046\u306b\u306a\u308b\u306e\u306b\u5bfe\u3057\u3001<\/p>\n<pre>00401037  xor         eax,eax \r\n00401039  lea         esp,[esp] \r\n00401040  mov         dword ptr [a (40337Ch)],eax \r\n00401045  add         eax,1 \r\n00401048  cmp         eax,40000000h \r\n0040104D  jl          main+40h (401040h) <\/pre>\n<p>\u5f8c\u308d\u306e\u30eb\u30fc\u30d7\u3067\u306f\u3001<\/p>\n<pre>00401101  mov         dword ptr [esp+20h],ebp \r\n00401105  mov         edx,dword ptr [esp+20h] \r\n00401109  cmp         edx,40000000h \r\n0040110F  jge         main+12Bh (40112Bh) \r\n00401111  mov         eax,dword ptr [esp+20h] \r\n00401115  mov         dword ptr [a (40337Ch)],eax \r\n0040111A  add         dword ptr [esp+20h],1 \r\n0040111F  mov         ecx,dword ptr [esp+20h] \r\n00401123  cmp         ecx,40000000h \r\n00401129  jl          main+111h (401111h) <\/pre>\n<p>\u3068\u306a\u3063\u3066\u3044\u305f\u3002\u305f\u3057\u304b\u306b\u6bce\u5ea6\u8aad\u307f\u8fbc\u3093\u3067\u3044\u308b\u3053\u3068\u304c\u308f\u304b\u308b\u3002(\u3061\u306a\u307f\u306b\u3001Release\u6700\u9069\u5316\u30d3\u30eb\u30c9)<\/p>\n<p>\u3053\u3061\u3089\u306evolatile\u3068\u3044\u3046\u82f1\u8a9e\u306e\u610f\u5473\u306f\u3001<a href=\"http:\/\/dictionary.goo.ne.jp\/search.php?MT=volatile&amp;kind=ej&amp;mode=0&amp;base=1&amp;row=0\" rel=\"nofollow\">goo\u8f9e\u66f8<\/a>\u3092\u53c2\u7167\u3057\u3066<\/p>\n<blockquote>\n<p class=\"quotation\">\u63ee\u767a\u6027\u306e; \u79fb\u308a\u6c17\u306e<\/p>\n<\/blockquote>\n<p>\u3068\u3044\u3046\u610f\u5473\u3089\u3057\u3044\u3002\u63ee\u767a\u6027\u3068\u3044\u3046\u306e\u306f\u3088\u304f\u805e\u304f\u304c\u3001\u79fb\u308a\u6c17\u306e\u3068\u3046\u610f\u5473\u306fmutable\u3068\u304b\u3076\u3063\u3066\u3044\u308b\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>mutable mutable\u306f\u3001\u30af\u30e9\u30b9\u306econst\u30e1\u30f3\u30d0\u95a2\u6570(\u30e1\u30bd\u30c3\u30c9)\u306e\u4e2d\u3067 &hellip; <a href=\"https:\/\/soundengine.jp\/wordpress\/tips\/tutorial\/329\/\">\u7d9a\u304d\u3092\u8aad\u3080 <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-329","post","type-post","status-publish","format-standard","hentry","category-tutorial"],"_links":{"self":[{"href":"https:\/\/soundengine.jp\/wordpress\/tips\/wp-json\/wp\/v2\/posts\/329","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/soundengine.jp\/wordpress\/tips\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/soundengine.jp\/wordpress\/tips\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/soundengine.jp\/wordpress\/tips\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/soundengine.jp\/wordpress\/tips\/wp-json\/wp\/v2\/comments?post=329"}],"version-history":[{"count":6,"href":"https:\/\/soundengine.jp\/wordpress\/tips\/wp-json\/wp\/v2\/posts\/329\/revisions"}],"predecessor-version":[{"id":3319,"href":"https:\/\/soundengine.jp\/wordpress\/tips\/wp-json\/wp\/v2\/posts\/329\/revisions\/3319"}],"wp:attachment":[{"href":"https:\/\/soundengine.jp\/wordpress\/tips\/wp-json\/wp\/v2\/media?parent=329"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/soundengine.jp\/wordpress\/tips\/wp-json\/wp\/v2\/categories?post=329"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/soundengine.jp\/wordpress\/tips\/wp-json\/wp\/v2\/tags?post=329"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}