{"id":1647,"date":"2011-12-01T23:20:51","date_gmt":"2011-12-01T15:20:51","guid":{"rendered":"http:\/\/blog.pighead.cc\/whsyu\/?p=1647"},"modified":"2011-12-03T01:18:24","modified_gmt":"2011-12-02T17:18:24","slug":"try-grand-central-dispatch","status":"publish","type":"post","link":"https:\/\/blog.pighead.cc\/whsyu\/2011\/12\/01\/try-grand-central-dispatch\/","title":{"rendered":"Grand Central Dispatch \u8a66\u73a9"},"content":{"rendered":"<p class=\"bb-post-separator\"><strong>[\u83ef\u8a9e, cmn-Hant-TW]  <\/strong><\/p><p>\u56e0\u70ba\u958b\u6709\u591a\u57f7\u884c\u7dd2\u7684\u9700\u6c42\uff0c\u5c31\u7814\u7a76\u4e86\u4e00\u4e0b mac \u7684 framework \u6709\u63d0\u4f9b\u54ea\u4e9b\u6771\u897f\u53ef\u4ee5\u7528\u3002\u521d\u6b65\u770b\u4f86\u5c31\u662f\u50b3\u7d71 pthread \u8ddf NSThread \u53ef\u4ee5\u7528\uff0c\u4e0d\u904e Thread \u7a0b\u5f0f\u5beb\u8d77\u4f86\u4e00\u5411\u5c31\u5f88\u7169\uff0c\u6240\u4ee5\u5c31\u60f3\u627e\u627e\u6709\u5565\u66ff\u4ee3\u54c1\u53ef\u4ee5\u7528\uff0c\u5f8c\u4f86\u5c31\u6ce8\u610f\u5230 Mac OS X 10.6 (iOS 4)\u4e4b\u5f8c\u65b0\u52a0\u7684\u00a0Grand Central Dispatch\u3002<\/p>\n<p>\u57fa\u672c\u6982\u5ff5\u5f88\u7c21\u55ae\uff0c\u5c31\u662f\u628a\u4f60\u8981\u505a\u7684\u6771\u897f\u52a0\u9032 queue \u5c31\u597d\u4e86\u3002\u628a\u8981\u505a\u7684\u4e8b\u60c5\u653e\u5728\u540c\u4e00\u500b queue \u7684\u8a71\u4ed6\u5c31\u6703\u7167\u9806\u5e8f\u4e00\u500b\u4e00\u500b\u57f7\u884c\uff0c\u7576\u7136\u4f60\u53ef\u4ee5\u958b\u5f88\u591a\u500b queue \u540c\u6642\u8dd1\uff0c\u90a3\u5c31\u53ef\u4ee5\u4e00\u6b21\u505a\u5f88\u591a\u4e8b\u60c5\uff0c\u76f4\u5230\u628a\u7cfb\u7d71\u69a8\u4e7e\u70ba\u6b62\u3002<\/p>\n<h5>\u9996\u5148\u8981\u958b\u4e00\u500b queue<\/h5>\n<pre lang=\"objc\">dispatch_queue_t queue = dispatch_queue_create(\"cc.pighead.queue\", NULL);<\/pre>\n<p>\u7b2c\u4e00\u500b\u53c3\u6578\u53ef\u4ee5\u96a8\u4fbf\u6253\uff0c\u7b2c\u4e8c\u500b\u53c3\u6578\u5728 10.6 \u6c92\u7528\uff0c10.7 \u4ee5\u4e0a\u624d\u6709\u7528\u9014\u3002<\/p>\n<h5>\u7136\u5f8c\u628a\u8981\u57f7\u884c\u7684\u7a0b\u5f0f\u78bc\u653e\u9032 queue \u88e1\u9762<\/h5>\n<pre lang=\"objc\">dispatch_async(queue, ^{\r\n  printf(\"this is a block!\\n\");\r\n});<\/pre>\n<p>\u88e1\u9762\u5305\u4f4f\u7684\u6771\u897f\u53eb block\uff0c\u81f3\u65bc block \u662f\u5565\u9019\u908a\u5148\u4e0d\u7ba1\u4ed6\uff0c\u53cd\u6b63\u5c31\u662f\u5305\u5728\u88e1\u9762\u7684\u7a0b\u5f0f\u78bc\u6703\u88ab\u53e6\u5916\u958b\u4e00\u500b\u57f7\u884c\u7dd2\u4f86\u8dd1\u5c31\u662f\u3002<\/p>\n<h5>\u7136\u5f8c\u628a\u4ed6 sync \u56de\u4f86<\/h5>\n<pre lang=\"objc\">dispatch_sync(queue, ^{});<\/pre>\n<h5>\u6700\u5f8c release \u6389<\/h5>\n<pre lang=\"objc\">dispatch_release(queue);<\/pre>\n<p>\u8981\u540c\u6642\u8dd1\u5f88\u591a\u7684\u8a71\u5c31\u591a\u958b\u5e7e\u500b\u5c31\u53ef\u4ee5\u4e86\uff0c\u60f3\u958b\u5e7e\u500b\u5c31\u958b\u5e7e\u500b\uff0c\u5728 sync \u4e4b\u524d\u6240\u6709\u7684 queue \u90fd\u53ef\u4ee5\u540c\u6642\u8dd1\uff0c\u5982\u679c\u6709\u8b8a\u6578\u6703\u5171\u7528\u7684\u8a71\u8a18\u5f97\u7528 lock \u6216\u662f @sychronized \u9019\u4e9b\u7684\u6771\u897f\u4f86\u63a7\u7ba1\uff0c\u5927\u81f4\u4e0a\u5c31\u662f\u9019\u6a23\u3002\u53e6\u5916\u9084\u6709 queue \u7fa4\u7d44\u8ddf for loop \u62c6\u89e3\u6210\u5e73\u884c\u8655\u7406\u7684\u7528\u6cd5\uff0c\u9019\u6b21\u6c92\u7528\u5230\u6240\u4ee5\u5c31\u6c92\u7279\u5225\u53bb\u6ce8\u610f\u4e86\u3002<\/p>\n<p>\u8a71\u8aaa FreeBSD \u4e0a\u4e5f\u662f\u53ef\u4ee5\u7528 Grand Central Dispatch \u7684\uff0c\u7528\u6cd5\u4e5f\u5dee\u4e0d\u591a\uff0c\u9700\u8981\u7684\u662f\u65b0\u4e00\u9ede\u7684 kernel\u3001llvm\/clang \u7de8\u8b6f\u5668\u4ee5\u53ca devel\/libdispatch \u9019\u500b port\u3002<\/p>\n<h3><strong>\u53c3\u8003\u6587\u4ef6<\/strong><\/h3>\n<p><a href=\"http:\/\/www.fieryrobot.com\/blog\/2010\/06\/27\/a-simple-job-queue-with-grand-central-dispatch\/\">A Simple Job Queue With Grand Central Dispatch<\/a><\/p>\n<p><a href=\"http:\/\/www.raywenderlich.com\/4295\/multithreading-and-grand-central-dispatch-on-ios-for-beginners-tutorial\">Multithreading and Grand Central Dispatch on iOS for Beginners Tutorial<\/a><\/p>\n<p><a href=\"http:\/\/blog.aki-null.net\/?p=46\">Nullified Construction<\/a><\/p>\n<p><a href=\"http:\/\/alienryderflex.com\/NSLock.html\">Mac OS X (Cocoa) NSLock<\/a><\/p>\n<p><a href=\"http:\/\/wiki.freebsd.org\/GCD\">GCD on FreeBSD<\/a><\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>[\u83ef\u8a9e, cmn-Hant-TW] \u56e0\u70ba\u958b\u6709\u591a\u57f7\u884c\u7dd2\u7684\u9700\u6c42\uff0c\u5c31\u7814\u7a76\u4e86\u4e00\u4e0b mac \u7684 framework \u6709\u63d0\u4f9b\u54ea\u4e9b\u6771\u897f\u53ef\u4ee5\u7528\u3002\u521d\u6b65\u770b\u4f86\u5c31\u662f\u50b3\u7d71 pthread \u8ddf NSThread \u53ef\u4ee5\u7528\uff0c\u4e0d\u904e Thread \u7a0b\u5f0f\u5beb\u8d77\u4f86\u4e00\u5411\u5c31\u5f88\u7169\uff0c\u6240\u4ee5\u5c31\u60f3\u627e\u627e\u6709\u5565\u66ff\u4ee3\u54c1\u53ef\u4ee5\u7528\uff0c\u5f8c\u4f86\u5c31\u6ce8\u610f\u5230 Mac OS X 10.6 (iOS 4)\u4e4b\u5f8c\u65b0\u52a0\u7684\u00a0Grand Central Dispatch\u3002 \u57fa\u672c\u6982\u5ff5\u5f88\u7c21\u55ae\uff0c\u5c31\u662f\u628a\u4f60\u8981\u505a\u7684\u6771\u897f\u52a0\u9032 queue \u5c31\u597d\u4e86\u3002\u628a\u8981\u505a\u7684\u4e8b\u60c5\u653e\u5728\u540c\u4e00\u500b queue \u7684\u8a71\u4ed6\u5c31\u6703\u7167\u9806\u5e8f\u4e00\u500b\u4e00\u500b\u57f7\u884c\uff0c\u7576\u7136\u4f60\u53ef\u4ee5\u958b\u5f88\u591a\u500b queue \u540c\u6642\u8dd1\uff0c\u90a3\u5c31\u53ef\u4ee5\u4e00\u6b21\u505a\u5f88\u591a\u4e8b\u60c5\uff0c\u76f4\u5230\u628a\u7cfb\u7d71\u69a8\u4e7e\u70ba\u6b62\u3002 \u9996\u5148\u8981\u958b\u4e00\u500b queue dispatch_queue_t queue = dispatch_queue_create(&#8220;cc.pighead.queue&#8221;, NULL); \u7b2c\u4e00\u500b\u53c3\u6578\u53ef\u4ee5\u96a8\u4fbf\u6253\uff0c\u7b2c\u4e8c\u500b\u53c3\u6578\u5728 10.6 \u6c92\u7528\uff0c10.7 \u4ee5\u4e0a\u624d\u6709\u7528\u9014\u3002 \u7136\u5f8c\u628a\u8981\u57f7\u884c\u7684\u7a0b\u5f0f\u78bc\u653e\u9032&hellip; <\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[200],"tags":[4591,258,168,4507],"class_list":["post-1647","post","type-post","status-publish","format-standard","hentry","category-development","tag-freebsd","tag-ios","tag-mac-os-x","tag-objective-c"],"_links":{"self":[{"href":"https:\/\/blog.pighead.cc\/whsyu\/wp-json\/wp\/v2\/posts\/1647","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.pighead.cc\/whsyu\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.pighead.cc\/whsyu\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.pighead.cc\/whsyu\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.pighead.cc\/whsyu\/wp-json\/wp\/v2\/comments?post=1647"}],"version-history":[{"count":14,"href":"https:\/\/blog.pighead.cc\/whsyu\/wp-json\/wp\/v2\/posts\/1647\/revisions"}],"predecessor-version":[{"id":1675,"href":"https:\/\/blog.pighead.cc\/whsyu\/wp-json\/wp\/v2\/posts\/1647\/revisions\/1675"}],"wp:attachment":[{"href":"https:\/\/blog.pighead.cc\/whsyu\/wp-json\/wp\/v2\/media?parent=1647"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.pighead.cc\/whsyu\/wp-json\/wp\/v2\/categories?post=1647"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.pighead.cc\/whsyu\/wp-json\/wp\/v2\/tags?post=1647"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}