{"id":594,"date":"2014-09-14T21:47:00","date_gmt":"2014-09-14T13:47:00","guid":{"rendered":"http:\/\/blog.qdac.cc\/?p=594"},"modified":"2014-09-16T16:21:33","modified_gmt":"2014-09-16T08:21:33","slug":"qworker%e6%9b%b4%e6%96%b0-%e5%a2%9e%e5%8a%a0%e5%af%b9for%e5%be%aa%e7%8e%af%e5%b9%b6%e8%a1%8c%e8%ae%a1%e7%ae%97%e6%94%af%e6%8c%81%ef%bc%88%e5%86%85%e6%b5%8b%ef%bc%89","status":"publish","type":"post","link":"https:\/\/blog.qdac.cc\/?p=594","title":{"rendered":"QWorker\u66f4\u65b0-\u589e\u52a0\u5bf9For\u5faa\u73af\u5e76\u884c\u8ba1\u7b97\u652f\u6301"},"content":{"rendered":"<p>QWorker\u65b0\u589e\u4e86\u548cXE7\u7684System.Threading\u5355\u5143\u7684For\u5faa\u73af\u5e76\u53d1\u652f\u6301\uff0c\u65b9\u6cd5\u662fTQForJobs.For(xxx)\u3002<strong>XE7\u4e2d\u7684For\u5faa\u73af\u5728\u7b49\u5f85\u4f5c\u4e1a\u5b8c\u6210\u524d\uff0c\u5982\u679c\u5728\u4e3b\u7ebf\u7a0b\u4e2d\u963b\u585e\u6d88\u606f\u5faa\u73af\u5904\u7406\uff0c\u8fd9\u6837\u5b50\uff0c\u4f1a\u9020\u6210\u754c\u9762\u7684\u5047\u6b7b\u3002QWorker\u7684For\u5faa\u73af\u89e3\u51b3\u4e86\u8fd9\u4e00\u95ee\u9898\uff0cAMsgWait\u53c2\u6570\u8bbe\u7f6e\u4e3aTrue\u65f6\uff0c\u5982\u679c\u5f53\u524d\u7b49\u5f85\u5904\u4e8e\u4e3b\u7ebf\u7a0b\u4e2d\u65f6\uff0c\u4f1a\u4f7f\u7528\u6d88\u606f\u5faa\u73af\u7b49\u5f85\uff1b\u5982\u679c\u4e0d\u5904\u4e8e\u4e3b\u7ebf\u7a0b\uff0c\u5219\u6b7b\u7b49\u3002\u540c\u65f6QWorker\u7684For\u8bed\u6cd5\u652f\u63012007-XE7\u7684\u6240\u6709\u7248\u672c\u7684Delphi\uff0c\u800c\u4e0d\u4ec5\u4ec5\u5c40\u9650\u4e8eXE7\u3002<\/strong><\/p>\n<p>\u5148\u770b\u4e0bQWorkerk\u4e2dFor\u7684\u51fd\u6570\u8bed\u6cd5\uff1a<\/p>\n<pre class=\"lang:delphi decode:true\">class function TQForJobs.&amp;For(const AStartIndex, AStopIndex: NativeInt;\r\n  AWorkerProc: TQForJobProc; AMsgWait: Boolean; AData: Pointer;\r\n  AFreeType: TQJobDataFreeType): TWaitResult;\r\nclass function TQForJobs.&amp;For(const AStartIndex, AStopIndex: NativeInt;\r\n  AWorkerProc: TQForJobProcA; AMsgWait: Boolean; AData: Pointer;\r\n  AFreeType: TQJobDataFreeType): TWaitResult;\r\nclass function TQForJobs.&amp;For(const AStartIndex, AStopIndex: NativeInt;\r\n  AWorkerProc: TQForJobProcG; AMsgWait: Boolean; AData: Pointer;\r\n  AFreeType: TQJobDataFreeType): TWaitResult;<\/pre>\n<h4>\u3010\u53c2\u6570\u8bf4\u660e\u3011<\/h4>\n<p>AStartIndex\uff1a\u5faa\u73af\u8d77\u59cb\u503c<\/p>\n<p>AStopIndex\uff1a\u5faa\u73af\u7ed3\u675f\u503c<\/p>\n<p>AWorkerProc\uff1a\u4f5c\u4e1a\u5904\u7406\u8fc7\u7a0b\uff0c\u6709\u4e09\u79cd\u91cd\u8f7d\uff0c\u5206\u522b\u5bf9\u5e94\u7c7b\u6210\u5458\u51fd\u6570\u3001\u533f\u540d\u51fd\u6570\u548c\u5168\u5c40\u51fd\u6570\u7248\u672c\u3002<\/p>\n<p>AMsgWait\uff1a\u662f\u5426\u5728\u7b49\u5f85\u65f6\u5141\u8bb8\u6d88\u606f\u5faa\u73af\uff0c\u5982\u679c\u4e3aTrue\uff0c\u5219\u4e3b\u7ebf\u7a0b\u4e0d\u4f1a\u88ab\u963b\u585e\uff0c\u6d88\u606f\u80fd\u591f\u6b63\u5e38\u5904\u7406\uff0c\u7a0b\u5e8f\u81ea\u5df1\u5904\u7406\u5fc5\u8981\u7684\u963b\u585e\uff0c\u5426\u5219\uff0c\u4e3b\u7ebf\u7a0b\u88ab\u963b\u585e\u3002<\/p>\n<p>AData\uff1a\u4f5c\u4e1a\u9644\u52a0\u6570\u636e\uff0c\u88ab\u5f53\u505a\u4f5c\u4e1a\u7684Data\u6210\u5458\u4f20\u9012\u7ed9\u4f5c\u4e1a\u5904\u7406\u51fd\u6570\u3002<\/p>\n<p>AFreeType\uff1a\u4f5c\u4e1a\u9644\u52a0\u6570\u636e\u7c7b\u578b\uff0c\u7528\u4e8e\u51b3\u5b9a\u662f\u5426\u81ea\u52a8\u91ca\u653e\u4f5c\u4e1a\u7684\u9644\u52a0\u6570\u636e\uff0c\u9ed8\u8ba4\u7531\u7528\u6237\u8d1f\u8d23\u91ca\u653e\u3002<\/p>\n<h4>\u3010\u8fd4\u56de\u503c\u3011<\/h4>\n<p>\u8fd4\u56de\u5faa\u73af\u7ed3\u679c\uff0c\u5982\u679c\u6210\u529f\u4eceAStartIndex\u5faa\u73af\u5230AStopIndex\uff0c\u5219\u8fd4\u56dewrSignaled\uff0c\u5982\u679c\u88ab\u4e2d\u9014\u53d6\u6d88\uff08\u8c03\u7528Break\uff0c\u6216\u8005\u7a0b\u5e8f\u9000\u51fa\uff09\uff0c\u5219\u8fd4\u56dewrAbandoned\u3002<\/p>\n<h4>\u3010\u6ce8\u610f\u3011<\/h4>\n<p>AStopIndex\u5e94\u8be5\u5927\u4e8e\u7b49\u4e8eAStartIndex\uff0c\u5426\u5219\u5faa\u73af\u8fc7\u7a0b\u5c06\u4e0d\u4f1a\u88ab\u6267\u884c\u3002\u4e0d\u8c61\u666e\u901a\u7684for\u8bed\u53e5\u6709downto\u6765\u9006\u5e8f\uff0cTQForJobs.For\u51fd\u6570\u4e0d\u652f\u6301\u9006\u5e8f\u3002<\/p>\n<h4>\u3010\u5907\u6ce8\u3011<\/h4>\n<p>\u6211\u4eec\u770b\u4e0bFor\u4f5c\u4e1a\u8fc7\u7a0b\u4e0e\u666e\u901a\u7684\u4f5c\u4e1a\u8fc7\u7a0b\u7684\u4e0d\u540c\uff1a<\/p>\n<pre class=\"lang:delphi decode:true \">TQForJobProc = procedure(ALoopMgr: TQForJobs; AJob: PQJob; AIndex: NativeInt) of object;\r\nTQForJobProcG = procedure(ALoopMgr: TQForJobs; AJob: PQJob;AIndex: NativeInt);\r\nTQForJobProcA = reference to procedure(ALoopMgr: TQForJobs; AJob: PQJob;AIndex: NativeInt);<\/pre>\n<p>\u4e0e\u666e\u901a\u7684\u4f5c\u4e1a\u8fc7\u7a0b\u76f8\u6bd4\uff0cFor\u5faa\u73af\u4f5c\u4e1a\u8fc7\u7a0b\u591a\u4e86\u4e24\u4e2a\u53c2\u6570\uff1a<\/p>\n<p>\uff081\uff09\u3001ALoopMgr\u53c2\u6570\u662f\u5faa\u73af\u4f5c\u4e1a\u7684\u63a7\u5236\u5bf9\u8c61\uff0c\u5b83\u63d0\u4f9b\u4e86\u4e00\u4e2aBreakIt\u65b9\u6cd5\u7528\u4e8e\u4e2d\u65ad\u5faa\u73af\u7684\u6267\u884c\uff1b<\/p>\n<p>\uff082\uff09\u3001AIndex\u53c2\u6570\u6307\u660e\u4e86\u5f53\u524d\u5faa\u73af\u7684\u7d22\u5f15\u503c\uff0c\u8868\u660e\u5b83\u662f\u7b2c\u51e0\u4e2a\u5faa\u73af\u3002<\/p>\n<p>\u81f3\u4e8eAJob\u53c2\u6570\uff0c\u5176\u542b\u4e49\u4e0e\u666e\u901a\u7684\u4f5c\u4e1a\u6ca1\u6709\u4efb\u4f55\u533a\u522b\u3002<\/p>\n<h4>\u3010\u793a\u4f8b\u3011<\/h4>\n<p>\u4e0b\u9762\u7684\u4f8b\u5b50\u662f\u533f\u540d\u51fd\u6570\u7248\u672c\uff1a<\/p>\n<pre class=\"lang:delphi decode:true\" title=\"\u533f\u540d\u51fd\u6570\u7248\u672c\">var\r\n  ARuns:NativeInt;\r\n  T,T1:Cardinal;\r\n  I: Integer;\r\nbegin\r\nARuns:=0;\r\nT:=GetTickCount;\r\nTQForJobs.For(0,999999,\r\n  procedure (AMgr: TQForJobs; AJob: PQJob; AIndex: NativeInt)\r\n  begin\r\n  AtomicIncrement(ARuns);\r\n  end\r\n  ,False,nil);\r\nT:=GetTickCount-T;\r\nShowMessage(IntToStr(T)+'ms');\r\nend;<\/pre>\n<p>\u4e0b\u9762\u7684\u4f8b\u5b50\u662f\u7c7b\u6210\u5458\u51fd\u6570\u7248\u672c\uff1a<\/p>\n<pre class=\"lang:delphi decode:true\" title=\"\u7c7b\u6210\u5458\u51fd\u6570\u7248\u672c\">procedure TForm1.DoForJobProc(AMgr: TQForJobs; AJob: PQJob; AIndex: NativeInt);\r\nbegin\r\nInterlockedIncrement(PInteger(AJob.Data)^);\r\nend;\r\n\r\nprocedure TForm1.Button1Click(Sender:TObject);\r\nvar\r\n  ARuns:NativeInt;\r\n  T,T1:Cardinal;\r\n  I: Integer;\r\nbegin\r\nARuns:=0;\r\nT:=GetTickCount;\r\nTQForJobs.For(0,999999,DoForJobProc,False,@ARuns);\r\nT:=GetTickCount-T;\r\nShowMessage(IntToStr(T)+'ms');\r\nend;<\/pre>\n<p>\u5168\u5c40\u51fd\u6570\u7248\u672c\u4e0e\u7c7b\u6210\u5458\u51fd\u6570\u7248\u672c\u4e00\u81f4\uff0c\u53ea\u662f\u4f5c\u4e1a\u8fc7\u7a0b\u53d8\u6210\u5168\u5c40\u51fd\u6570\u800c\u5df2\uff0c\u5c31\u4e0d\u5570\u55e6\u4e86\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>QWorker\u65b0\u589e\u4e86\u548cXE7\u7684Syste [&hellip;]<\/p>\n","protected":false},"author":1,"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,6],"tags":[],"class_list":["post-594","post","type-post","status-publish","format-standard","hentry","category-delphi","category-qworker"],"views":4870,"_links":{"self":[{"href":"https:\/\/blog.qdac.cc\/index.php?rest_route=\/wp\/v2\/posts\/594","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\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.qdac.cc\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=594"}],"version-history":[{"count":14,"href":"https:\/\/blog.qdac.cc\/index.php?rest_route=\/wp\/v2\/posts\/594\/revisions"}],"predecessor-version":[{"id":630,"href":"https:\/\/blog.qdac.cc\/index.php?rest_route=\/wp\/v2\/posts\/594\/revisions\/630"}],"wp:attachment":[{"href":"https:\/\/blog.qdac.cc\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=594"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.qdac.cc\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=594"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.qdac.cc\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=594"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}