{"id":3966,"date":"2016-06-16T15:55:53","date_gmt":"2016-06-16T07:55:53","guid":{"rendered":"http:\/\/blog.qdac.cc\/?p=3966"},"modified":"2016-06-16T15:55:53","modified_gmt":"2016-06-16T07:55:53","slug":"%e7%bf%bb%e8%af%91%e4%bf%ae%e5%a4%8d-tcriticalsection","status":"publish","type":"post","link":"https:\/\/blog.qdac.cc\/?p=3966","title":{"rendered":"[\u7ffb\u8bd1]\u4fee\u590d TCriticalSection"},"content":{"rendered":"<p><span style=\"color: #ff0000\">\u6ce8\uff1a\u57fa\u4e8e\u672c\u4eba\u82f1\u6587\u6c34\u5e73\uff0c\u4ee5\u4e0b\u7ffb\u8bd1\u53ea\u662f\u6211\u81ea\u5df1\u7684\u7406\u89e3\uff0c\u5982\u5bf9\u8bfb\u8005\u9020\u6210\u672a\u77e5\u5f71\u54cd\uff0c\u4e00\u5207\u540e\u679c\u81ea\u8d1f\u3002\u5982\u679c\u53d1\u73b0\u6709\u7ffb\u8bd1\u9519\u8bef\u7684\uff0c\u6b22\u8fce\u6307\u6b63\u3002<\/span><\/p>\n<p>\u539f\u6587\u5730\u5740\uff1a<a href=\"https:\/\/www.delphitools.info\/2011\/11\/30\/fixing-tcriticalsection\">https:\/\/www.delphitools.info\/2011\/11\/30\/fixing-tcriticalsection<\/a><\/p>\n<p><strong>TCriticalSection<\/strong>(TMonitor)\u6709\u4e25\u91cd\u8bbe\u8ba1\u7f3a\u9677\uff0c\u5728 \u8fdb\u5165\/\u79bb\u5f00 \u4e0d\u540c\u7684TCriticalSection \u5b9e\u4f8b\u65f6\u53ef\u80fd\u7ed3\u675f\u4f60\u7684\u7ebf\u7a0b, \u751a\u81f3\u4f60\u7684\u7ebf\u7a0b\u5982\u679c\u662f\u5e8f\u5217\u5316\u7684\uff0c\u7ebf\u7a0b\u4f1a\u7ed3\u675f\u6267\u884c\u3002<\/p>\n<p><span style=\"color: #ff9900\">(\u6ce8\uff1a\u8fd9\u6bb5\u5f88\u7ed5\uff0c\u6211\u7406\u89e3\u5c31\u662f\u8bf4\u4f60\u6709\u5f88\u591a\u4e34\u754c\u533a\u65f6\uff0c\u53ef\u80fd\u4f1a\u5bfc\u81f4\u7ebf\u7a0b\u505c\u6b62)<\/span><\/p>\n<p>\u8fd9\u662f\u56e0\u4e3aTCriticalSection\u5b9e\u4f8b\u662f\u5c0f\u7684\uff0c\u52a8\u6001\u5206\u914d\u7684\u5bf9\u8c61, \u6240\u4ee5\u51e0\u4e2aTCriticalSection\u5b9e\u4f8b\u53ef\u4ee5\u653e\u5728\u540c\u4e00\u4e2aCPU cache line, \u5f53\u8fd9\u79cd\u884c\u4e3a\u53d1\u751f\u65f6, \u4f60\u4f1a\u6709\u5f88\u591a\u7f13\u5b58\u51b2\u7a81\u5728\u5185\u6838\u548c\u7ebf\u7a0b\u4e4b\u95f4\u3002<\/p>\n<p>\u8fd9\u662f\u6709\u591a\u4e25\u91cd? \u597d\u5427, \u8fd9\u53d6\u51b3\u4e8e\u4f60\u7684CPU\u6709\u591a\u5c11\u6838\uff0c\u4f60\u6709\u66f4\u591a\u7684\u6838\uff0c\u5c31\u66f4\u5bb9\u6613\u4ea7\u751f\u3002\u5728\u4e00\u4e2a4\u6838CPU\u4e0a\uff0c\u6700\u574f\u7684\u60c5\u51b5\u662f\u5bfc\u81f4200%\u7684\u901f\u5ea6\u653e\u6162\uff0c\u5e76\u4e14\u4ed6\u4e0d\u4f1a\u91cd\u590d<span style=\"color: #ff9900\">\uff08\u6ce8\uff1a\u610f\u601d\u662f\u968f\u673a\u53d1\u751f\u7684\u4e8b\u4ef6\uff09<\/span>\u3002<\/p>\n<p>\u503c\u5f97\u5e86\u5e78\u7684\u662f\uff0c\u6709\u4e00\u4e2a\u7b80\u5355\u7684\u89e3\u51b3\u65b9\u6cd5\uff0c\u4f7f\u7528<strong>TFixedCriticalSection<\/strong>:<\/p>\n<p><strong>type<\/strong><\/p>\n<p>TFixedCriticalSection = <strong>class<\/strong>(TCriticalSection)<\/p>\n<p><strong>private<\/strong><\/p>\n<p>FDummy : <strong>array<\/strong> [0..95] <strong>of Byte<\/strong>;<\/p>\n<p><strong>end<\/strong>;<\/p>\n<p>\u8fd9\u53ef\u4ee5\u786e\u4fdd\u5b9e\u4f8b\u7684\u5927\u5c0f\u5927\u4e8e96\u5b57\u8282\uff0c\u8fd9\u610f\u5473\u7740\u5b83\u4f1a\u8d85\u8fc7\u76ee\u524d\u6240\u6709\u7684CPU Cache line\uff0c\u6240\u4ee5\u6ca1\u6709\u4e86\u8de8\u4e0d\u540c\u7684\u4e34\u754c\u533a\u5b9e\u4f8b\u5e8f\u5217\u5316\u3002<\/p>\n<p>\u4f5c\u4e3a\u5956\u52b1\uff0c\u5b83\u8fd8\u4f7f\u7528\u4e86\u4e00\u4e2a\u66f4\u5927\u3001\u66f4\u4e00\u81f4\uff0cFastMM bucket(\u6876)\uff0c\u6027\u80fd\u6709\u5927\u69827%\u63d0\u5347\u3002\u7f3a\u70b9\u662f\u4f60\u4f7f\u7528\u4e86\u66f4\u591a\u7684\u5185\u5b58\uff0c\u4f46\u662f\u4f60\u771f\u7684\u6709\u5f88\u591acritical section(\u4e34\u754c\u533a)\u5b9e\u4f8b\u5417\uff1f<\/p>\n<p><strong>* (11-12-01): <\/strong><strong>\u63d0\u793a<\/strong><strong>,<\/strong><strong>\u5728<\/strong><strong>XE2<\/strong><strong>\u4e2d\uff0c<\/strong><strong>TMonitor <\/strong><strong>\u7684\u95ee\u9898\u88ab\u4fee\u590d\u4e86\u3002<\/strong><\/p>\n<p><span style=\"color: #ff9900\">(\u6ca1\u6709\u8bf4\u660eTcriticalSection\u662f\u5426\u5728XE2\u4ee5\u540e\u88ab\u4fee\u590d\u4e86)<\/span><\/p>\n<p>&nbsp;<\/p>\n<p><strong>\u5907\u6ce8\uff1a<\/strong><\/p>\n<p>1\uff1a CPU cache line\uff1a\uff08CPU\u7f13\u5b58\u9879?\uff09\u8fd9\u4e2a\u6211\u4e5f\u4e0d\u77e5\u9053\u662f\u4ec0\u4e48\uff0c\u4f30\u8ba1\u662fCPU\u7684\u7f13\u5b58\u7ba1\u7406\u6280\u672f\u3002<\/p>\n<p>2\uff1a\u5355\u6838\u6216\u4e34\u754c\u533a\u5bf9\u8c61\u7528\u5f97\u5c11\u7684\uff0c\u4f30\u8ba1\u9047\u5230\u8fd9\u79cd\u60c5\u51b5\u7684\u673a\u7387\u5f88\u5c11\u3002<\/p>\n<p>3\uff1a\u4f4e\u7248\u672c\u7684Delphi\u8fd8\u662f\u5efa\u8bae\u4fee\u590d\u4e00\u4e0b\u3002<\/p>\n<p>4\uff1a\u4e3a\u4ec0\u4e48\u662f96\u5b57\u8282\uff0c\u6211\u4e5f\u4e0d\u77e5\u9053\uff0c\u8fd9\u4e2a\u5df2\u7ecf\u5230\u4e86CPU\u786c\u4ef6\u8303\u7574\u3002<\/p>\n<p>5\uff1a\u4f5c\u8005\u8bf4 Class Helper\u4e0d\u80fd\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u3002<\/p>\n<p>6\uff1a\u4f5c\u8005\u8bf4TRTLCriticalSection\u00a0\u4e5f\u4f1a\u6709\u76f8\u4f3c\u95ee\u9898\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6ce8\uff1a\u57fa\u4e8e\u672c\u4eba\u82f1\u6587\u6c34\u5e73\uff0c\u4ee5\u4e0b\u7ffb\u8bd1\u53ea\u662f\u6211\u81ea\u5df1 [&hellip;]<\/p>\n","protected":false},"author":104,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[8],"tags":[],"class_list":["post-3966","post","type-post","status-publish","format-standard","hentry","category-delphi"],"views":4016,"_links":{"self":[{"href":"https:\/\/blog.qdac.cc\/index.php?rest_route=\/wp\/v2\/posts\/3966","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.qdac.cc\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.qdac.cc\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.qdac.cc\/index.php?rest_route=\/wp\/v2\/users\/104"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.qdac.cc\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3966"}],"version-history":[{"count":2,"href":"https:\/\/blog.qdac.cc\/index.php?rest_route=\/wp\/v2\/posts\/3966\/revisions"}],"predecessor-version":[{"id":3968,"href":"https:\/\/blog.qdac.cc\/index.php?rest_route=\/wp\/v2\/posts\/3966\/revisions\/3968"}],"wp:attachment":[{"href":"https:\/\/blog.qdac.cc\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3966"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.qdac.cc\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3966"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.qdac.cc\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3966"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}