{"id":1770,"date":"2015-01-26T00:20:02","date_gmt":"2015-01-25T16:20:02","guid":{"rendered":"http:\/\/blog.qdac.cc\/?p=1770"},"modified":"2015-01-26T00:20:02","modified_gmt":"2015-01-25T16:20:02","slug":"%e5%9f%ba%e4%ba%8e-qworker-%e7%9a%84%e5%a4%9a%e7%ba%bf%e7%a8%8b%e7%bc%96%e7%a8%8b-for-%e5%b9%b6%e8%a1%8c","status":"publish","type":"post","link":"https:\/\/blog.qdac.cc\/?p=1770","title":{"rendered":"\u57fa\u4e8e QWorker \u7684\u591a\u7ebf\u7a0b\u7f16\u7a0b &#8211; For \u5e76\u884c"},"content":{"rendered":"<p>\u770b\u5230\u8fd9\u4e2a\u6807\u9898\uff0c\u4f60\u53ef\u80fd\u4f1a\u611f\u89c9\u5230\u7591\u60d1\uff1a\u96be\u9053\u6211\u4eec\u524d\u9762\u7528 Post \u6216 TQJobGroup \u63d0\u4ea4\u7684\u4f5c\u4e1a\u4e0d\u662f\u5e76\u884c\u7684\u5417\uff1f\u4e3a\u4ec0\u4e48\u53c8\u4f1a\u5192\u51fa\u4e00\u4e2a For \u5e76\u884c\uff1f<\/p>\n<p>\u9996\u5148\uff0c\u7b2c\u4e00\u4e2a\u95ee\u9898\u7684\u7b54\u6848\u662f\uff1a\u5b83\u4eec\u5f53\u7136\u662f\u5e76\u884c\u7684\uff0c\u4f46\u5728\u7279\u5b9a\u7684\u573a\u5408\u4e0b\uff0c\u7531\u4e8e\u8c03\u5ea6\u4f1a\u5f15\u8d77\u4e00\u5b9a\u7684\u6027\u80fd\u635f\u5931\u3002<\/p>\n<p>\u63a5\u4e0b\u6765\uff0c\u7b2c\u4e8c\u4e2a\u95ee\u9898\u7684\u7b54\u6848\u662f\uff1aFor \u5e76\u884c\u662f\u5728\u7279\u5b9a\u573a\u5408\u4e0b\u7684\u89e3\u51b3\u65b9\u6848\uff0c\u5b83\u7684\u76ee\u7684\u662f\u6309\u6307\u5b9a\u7684\u6b21\u6570\u91cd\u590d\u6267\u884c\u6307\u5b9a\u7684\u4f5c\u4e1a\u5904\u7406\u51fd\u6570\u3002\u7531\u4e8e\u5b83\u4e0d\u9700\u8981\u91cd\u590d\u6295\u5bc4\u4f5c\u4e1a\uff0c\u6240\u4ee5\u8c03\u5ea6\u5f00\u9500\u66f4\u5c0f\uff0c\u5728\u6ee1\u8db3\u6761\u4ef6\u7684\u524d\u63d0\u4e0b\uff0c\u6027\u80fd\u66f4\u4f73\u3002<\/p>\n<p>\u6211\u4eec\u6765\u770b\u770b For \u5e76\u884c\u6709\u4ec0\u4e48\u9650\u5236\uff1a<\/p>\n<p>\uff081\uff09\u3001\u5b83\u53ea\u4f1a\u6bcf\u4e2aCPU\u4f7f\u7528\u4e00\u4e2a\u5de5\u4f5c\u8005\u5de5\u4f5c\u3002<\/p>\n<p>\uff082\uff09\u3001\u5b83\u5fc5\u9700\u7b49\u5f85\u6240\u6709\u7684\u4f5c\u4e1a\u5b8c\u6210\u6216\u8c03\u7528BreakIt\u4e2d\u6b62\u65f6\uff0c\u624d\u4f1a\u9000\u51fa\u6267\u884c\u3002<\/p>\n<p>\uff083\uff09\u3001\u5b83\u7684\u6240\u6709\u4f5c\u4e1a\u6267\u884c\u51fd\u6570\u90fd\u662f\u5728\u540e\u53f0\u7ebf\u7a0b\u4e2d\u7684\u6267\u884c\u7684\uff0c\u4e0d\u5141\u8bb8\u8c03\u5ea6\u4e3b\u7ebf\u7a0b\u4f5c\u4e1a\u3002<\/p>\n<p>\uff084\uff09\u3001\u540c\u65f6\u53ea\u6709\u4e00\u4e2a\u4f5c\u4e1a\u51fd\u6570\u5728\u5e76\u53d1\u6267\u884c\uff0c\u4e0d\u80fd\u540c\u65f6\u8fd0\u884c\u4e0d\u540c\u7684\u4f5c\u4e1a\u51fd\u6570\u3002<\/p>\n<p>\u800c\u4e0e For \u5e76\u884c\u76f8\u6bd4\uff0cTQJobGroup \u7684\u4e3b\u8981\u4f18\u52bf\u4f53\u73b0\u5728\uff1a<\/p>\n<p>\uff081\uff09\u3001\u66f4\u5927\u7684\u5e76\u53d1\u5904\u7406\u80fd\u529b\uff0c\u5b9e\u9645\u7684\u5de5\u4f5c\u8005\u6570\u91cf\u4ec5\u53d7\u4f5c\u4e1a\u6570\u91cf\u548c MaxWorkers \u5c5e\u6027\u9650\u5236\u3002<\/p>\n<p>\uff082\uff09\u3001\u66f4\u7075\u6d3b\u3002<\/p>\n<p>\uff082.1\uff09\u3001\u4f5c\u4e1a\u53ef\u4ee5\u5de5\u4f5c\u5728\u4e3b\u7ebf\u7a0b\u548c\u540e\u53f0\u7ebf\u7a0b\u4e2d\uff1b<\/p>\n<p>\uff082.2\uff09\u3001\u4f5c\u4e1a\u53ef\u4ee5\u987a\u5e8f\u6267\u884c\uff0c\u4e5f\u53ef\u4ee5\u5e76\u884c\u6267\u884c\uff1b<\/p>\n<p>\uff082.3\uff09\u3001\u53ef\u4ee5\u540c\u65f6\u8fd0\u884c\u4e0d\u540c\u7684\u4f5c\u4e1a\u51fd\u6570\u3002<\/p>\n<p>\u7efc\u4e0a\u6240\u8ff0\uff0c\u5728\u80fd\u591f\u4f7f\u7528 For \u5e76\u884c\u7684\u573a\u5408\uff0c\u6027\u80fd\u8981\u6bd4 TQJobGroup \u66f4\u9ad8\u3002\u6211\u4eec\u6839\u636e\u4e0a\u9762\u7684\u6bd4\u8f83\u53ef\u4ee5\u57fa\u672c\u786e\u5b9a\u4e00\u4e9b\u539f\u5219\uff1a<\/p>\n<p>\uff081\uff09\u3001For \u4e0d\u80fd\u7528\u4e8e\u9700\u8981\u5728\u4e3b\u7ebf\u7a0b\u4e2d\u6267\u884c\u7684\u4f5c\u4e1a\uff1b<\/p>\n<p>\uff082\uff09\u3001For \u9002\u7528\u4e8e\u540c\u65f6\u6267\u884c\u540c\u4e00\u4e2a\u51fd\u6570\u7684\u4f5c\u4e1a\uff0c\u540c\u65f6\u8fd0\u884c\u4e0d\u540c\u7684\u4f5c\u4e1a\u5904\u7406\u51fd\u6570\u9700\u8981\u4f7f\u7528 TQJobGroup\uff1b<\/p>\n<p>\uff083\uff09\u3001For \u4e0d\u80fd\u8981\u6c42\u4f5c\u4e1a\u51fd\u6570\u7684\u6267\u884c\u987a\u5e8f\uff1b<\/p>\n<p>\uff084\uff09\u3001For \u4e0d\u5e94\u7528\u4e8e\u6162\u901f IO \u7275\u6d89\u5230\u7684\u8ba1\u7b97\uff1b<\/p>\n<p>\u597d\u4e86\uff0c\u8bf4\u4e86\u8fd9\u4e48\u591a\uff0c\u6211\u4eec\u6765\u505a\u4e00\u4e2a\u5b9e\u9645\u7684 For \u5e76\u884c\u7684\u4f8b\u5b50\uff0c\u9996\u5148\u6765\u770b For \u5e76\u884c\u7684\u58f0\u660e\uff1a<\/p>\n<pre class=\"lang:delphi decode:true \">class function TQWorkers.&amp;For(const AStartIndex, AStopIndex: NativeInt;\r\n  AWorkerProc: TQForJobProc; AMsgWait: Boolean; AData: Pointer;\r\n  AFreeType: TQJobDataFreeType): TWaitResult;inline;\r\nclass function TQWorkers.&amp;For(const AStartIndex, AStopIndex: NativeInt;\r\n  AWorkerProc: TQForJobProcA; AMsgWait: Boolean; AData: Pointer;\r\n  AFreeType: TQJobDataFreeType): TWaitResult;inline;\r\nclass function TQWorkers.&amp;For(const AStartIndex, AStopIndex: NativeInt;\r\n  AWorkerProc: TQForJobProcG; AMsgWait: Boolean; AData: Pointer;\r\n  AFreeType: TQJobDataFreeType): TWaitResult;inline<\/pre>\n<p>\u4f60\u53ef\u80fd\u6ce8\u610f\u5230\u4e86\uff0c\u8fd9\u4e09\u4e2a For \u51fd\u6570\u90fd\u662f inline \u5185\u8054\u51fd\u6570\uff0c\u5b83\u5b9e\u9645\u4e0a\u89e6\u53d1\u7684\u662f TQForJobs \u7684\u65b9\u6cd5\uff0c\u8fd9\u53ea\u662f\u7edf\u4e00\u63a5\u53e3\u7684\u8bed\u6cd5\u7cd6\uff0c\u4e24\u8005\u672c\u8eab\u6ca1\u6709\u4efb\u4f55\u533a\u522b\u3002\u6211\u4eec\u6765\u4e86\u89e3\u4e0b\u5404\u4e2a\u53c2\u6570\u7684\u542b\u4e49\uff1a<\/p>\n<blockquote><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><\/blockquote>\n<p>\u5728\u6267\u884c\u5b8c\u6210\u540e\uff0c\u4f1a\u8fd4\u56de\u4e00\u4e2a\u7b49\u5f85\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<p><strong>\u3010\u6ce8\u610f\u3011\u5b9e\u9645\u5faa\u73af\u7684\u6267\u884c\u8303\u56f4\u662f\u96c6\u5408 [AStartIndex,AStopIndex]\uff0c\u8fd9\u610f\u5473\u7740AStopIndex\u5e94\u8be5\u5927\u4e8e\u7b49\u4e8eAStartIndex\uff0c\u5426\u5219\u5faa\u73af\u8fc7\u7a0b\u5c06\u4e0d\u4f1a\u88ab\u6267\u884c\u3002<\/strong><\/p>\n<p>\u90a3\u4e48\uff0c\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u770b\u4e0bFor\u4f5c\u4e1a\u51fd\u6570\u7684\u58f0\u660e\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<p>\u4e0b\u5728\u6211\u4eec\u6765\u770b\u5177\u4f53\u7684\u4f8b\u5b50\uff0c\u540c\u524d\u9762\u4e00\u6837\uff0c\u6211\u4eec\u7701\u7565\u4e86\u533f\u540d\u51fd\u6570\u548c\u5168\u5c40\u51fd\u6570\u7684\u7248\u672c\uff0c\u9700\u8981\u7684\u8bdd\uff0c\u60a8 \u53ea\u9700\u8981\u5c06\u76f8\u5173\u51fd\u6570\u76f4\u63a5\u66ff\u6362\u5373\u53ef\uff1a<\/p>\n<p>\u3010Delphi\u3011<\/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>\u3010C++ Builder\u3011<\/p>\n<pre class=\"lang:c++ decode:true \">void __fastcall TForm1::DoForJobProc(TQForJobs *AMgr,PQJob *AJob,NativeInt AIndex)\r\n{\r\nInterlockedIncrement(*((int *)AJob.Data));\r\n}\r\n\r\nvoid __fastcall TForm1::Button1Click(System::TObject *Sender);\r\n{\r\nNativeInt ARuns;\r\nDWORD T,T1;\r\nInteger I;\r\nARuns=0;\r\nT=GetTickCount();\r\nTQWorkers::For(0,999999,DoForJobProc,false,&amp;ARuns);\r\nT=GetTickCount()-T;\r\nShowMessage(IntToStr(T)+'ms');\r\n}<\/pre>\n<p>\u5173\u4e8e For \u5e76\u884c\uff0c\u672c\u6587\u6682\u65f6\u53ea\u8bf4\u8fd9\u4e48\u591a\uff0c\u5982\u679c\u6709\u4ec0\u4e48\u7591\u95ee\uff0c\u6b22\u8fce\u5728\u5b98\u65b9\u7fa4\u6216\u8005\u540e\u9762\u7559\u8a00\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u770b\u5230\u8fd9\u4e2a\u6807\u9898\uff0c\u4f60\u53ef\u80fd\u4f1a\u611f\u89c9\u5230\u7591\u60d1\uff1a\u96be\u9053\u6211 [&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":[69,8,9,6],"tags":[57],"class_list":["post-1770","post","type-post","status-publish","format-standard","hentry","category-c-builder","category-delphi","category-qdac","category-qworker","tag-57"],"views":3627,"_links":{"self":[{"href":"https:\/\/blog.qdac.cc\/index.php?rest_route=\/wp\/v2\/posts\/1770","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=1770"}],"version-history":[{"count":2,"href":"https:\/\/blog.qdac.cc\/index.php?rest_route=\/wp\/v2\/posts\/1770\/revisions"}],"predecessor-version":[{"id":1820,"href":"https:\/\/blog.qdac.cc\/index.php?rest_route=\/wp\/v2\/posts\/1770\/revisions\/1820"}],"wp:attachment":[{"href":"https:\/\/blog.qdac.cc\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1770"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.qdac.cc\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1770"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.qdac.cc\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1770"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}