{"id":1030,"date":"2015-08-11T11:00:25","date_gmt":"2015-08-11T02:00:25","guid":{"rendered":"https:\/\/blog.ymyzk.com\/?p=1030"},"modified":"2016-12-04T01:27:21","modified_gmt":"2016-12-03T16:27:21","slug":"gcd-grand-central-dispatch-semaphore","status":"publish","type":"post","link":"https:\/\/blog.ymyzk.com\/2015\/08\/gcd-grand-central-dispatch-semaphore\/","title":{"rendered":"GCD \u306e\u30c7\u30a3\u30b9\u30d1\u30c3\u30c1\u30bb\u30de\u30d5\u30a9\u3092\u6d3b\u7528\u3059\u308b (Objective-C\u301cSwift 3 \u5bfe\u5fdc)"},"content":{"rendered":"
Grand Central Dispatch (GCD)<\/a> \u306f Mac OS X 10.6+ \u3084 iOS 4+ \u3067\u5229\u7528\u51fa\u6765\u308b\u6280\u8853\u3067, \u30bf\u30b9\u30af\u3092\u7c21\u5358\u306b\u975e\u540c\u671f\u3067\u5b9f\u884c\u3055\u305b\u308b\u3053\u3068\u304c\u51fa\u6765\u308b\u6280\u8853\u306e\u4e00\u3064\u3067\u3059.<\/p>\n \u3053\u306e\u8a18\u4e8b\u3067\u306f GCD \u306e\u6a5f\u80fd\u306e\u4e00\u3064\u3067\u3042\u308b\u30c7\u30a3\u30b9\u30d1\u30c3\u30c1\u30bb\u30de\u30d5\u30a9\u3092\u6d3b\u7528\u3059\u308b\u65b9\u6cd5\u3092\u7d39\u4ecb\u3057\u307e\u3059. \u4f8b\u3068\u3057\u3066\u6709\u9650\u30ea\u30bd\u30fc\u30b9\u3078\u306e\u30a2\u30af\u30bb\u30b9\u5236\u9650\u3068, \u30bf\u30b9\u30af\u306e\u5b9f\u884c\u5f85\u3061\u3092\u53d6\u308a\u4e0a\u3052\u307e\u3059.<\/p>\n \u3053\u306e\u8aac\u660e\u3067\u306f Swift 3 \u3092\u5229\u7528\u3057\u3066\u3044\u307e\u3059. Swift 3 \u304b\u3089 API \u306e\u547d\u540d\u898f\u5247\u304c\u5927\u304d\u304f\u5909\u308f\u3063\u3066\u3044\u307e\u3059\u304c, \u540c\u3058\u6a5f\u80fd\u306f Swift 3 \u672a\u6e80\u3084 Objective-C \u3067\u3082\u5229\u7528\u3067\u304d\u307e\u3059.\u00a0Objective-C \u3084 Swift 1\/2 \u3067\u306e\u8a18\u6cd5\u306f\u3053\u306e\u8a18\u4e8b\u306e\u672b\u5c3e\u3092\u53c2\u7167\u3057\u3066\u304f\u3060\u3055\u3044. \u57fa\u672c\u7684\u306b\u30ab\u30a6\u30f3\u30c6\u30a3\u30f3\u30b0\u30bb\u30de\u30d5\u30a9<\/a>\u3068\u540c\u3058\u3067\u3059\u304c, \u3088\u308a\u52b9\u7387\u7684\u306b\u52d5\u4f5c\u3059\u308b\u3088\u3046\u306b\u5b9f\u88c5\u3055\u308c\u3066\u3044\u308b\u3068\u516c\u5f0f\u30c9\u30ad\u30e5\u30e1\u30f3\u30c8\u306b\u8a18\u8ff0\u3055\u308c\u3066\u3044\u307e\u3059.<\/p>\n \u5f93\u6765\u304b\u3089\u3042\u308b\u30bb\u30de\u30d5\u30a9\u3068\u4f3c\u3066\u3044\u307e\u3059\u304c\u3001\u3088\u308a\u52b9\u7387\u7684\u306b\u52d5\u4f5c\u3057\u307e\u3059\u3002\u30ab\u30fc\u30cd\u30eb\u6a5f\u80fd\u3092\u547c\u3073\u51fa\u3059\u306e\u306f\u3001\u30bb\u30de\u30d5\u30a9\u304c\u4f7f\u3048\u306a\u3044\u305f\u3081\u3001\u547c\u3073\u51fa\u3057\u5143\u306e\u30b9\u30ec\u30c3\u30c9\u3092\u30d6\u30ed\u30c3\u30af\u3057\u306a\u3051\u308c\u3070\u306a\u3089\u306a\u3044\u5834\u5408\u3060\u3051\u3060\u304b\u3089\u3067\u3059\u3002\u30bb\u30de\u30d5\u30a9\u304c\u4f7f\u3048\u308c\u3070\u3001\u30ab\u30fc\u30cd\u30eb\u547c\u3073\u51fa\u3057\u306f\u8d77\u3053\u308a\u307e\u305b\u3093\u3002 \u57fa\u672c\u7684\u306a\u64cd\u4f5c\u3068\u3057\u3066 \u30bb\u30de\u30d5\u30a9\u306e\u6d3b\u7528\u65b9\u6cd5\u3068\u3057\u3066\u6570\u304c\u6709\u9650\u306e\u30ea\u30bd\u30fc\u30b9\u306b\u5bfe\u3057\u3066, \u540c\u6642\u306b\u30a2\u30af\u30bb\u30b9\u3067\u304d\u308b\u6570\u3092\u5236\u9650\u3059\u308b\u3068\u3044\u3046\u3082\u306e\u304c\u3042\u308a\u307e\u3059. (\u4f8b: \u540c\u6642\u306b\u958b\u3051\u308b\u30d5\u30a1\u30a4\u30eb\u306e\u6570, \u901a\u4fe1\u306b\u304a\u3051\u308b\u540c\u6642\u63a5\u7d9a\u6570)<\/p>\n \u307e\u305a\u30bb\u30de\u30d5\u30a9\u3092\u4f7f\u308f\u306a\u3044\u5834\u5408\u306e\u4f8b\u3068\u3057\u3066, \u4ee5\u4e0b\u306e\u30b3\u30fc\u30c9\u3092\u6319\u3052\u307e\u3059. \u3053\u306e\u30d7\u30ed\u30b0\u30e9\u30e0\u3067\u306f\u30ed\u30b0\u30e1\u30c3\u30bb\u30fc\u30b8\u3092\u8868\u793a\u3057\u306a\u304c\u30893\u79d2\u9593\u5f85\u3064\u3068\u3044\u3046\u30bf\u30b9\u30af\u3092, \u30b0\u30ed\u30fc\u30d0\u30eb\u306a\u4e26\u5217\u30c7\u30a3\u30b9\u30d1\u30c3\u30c1\u30ad\u30e5\u30fc\u3092\u7528\u3044\u3066, 10\u500b\u4e26\u5217\u3067\u5b9f\u884c\u3057\u307e\u3059.<\/p>\n \u5b9f\u884c\u4f8b\u306f\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u306a\u308a\u307e\u3059. \u4e26\u5217\u30ad\u30e5\u30fc\u3092\u5229\u7528\u3057\u3066\u3044\u308b\u305f\u3081\u30bf\u30b9\u30af\u306e\u5b8c\u4e86\u3092\u5f85\u305f\u305a\u306b10\u500b\u306e\u30bf\u30b9\u30af\u304c\u4e26\u5217\u3067\u5b9f\u884c\u3055\u308c, 3\u79d2\u5f8c\u306b\u30bf\u30b9\u30af\u304c\u7d42\u4e86\u3057\u3066\u3044\u307e\u3059.<\/p>\n \u6b21\u306b\u30bb\u30de\u30d5\u30a9\u3092\u4f7f\u3063\u3066, \u540c\u6642\u306b2\u500b\u306e\u30bf\u30b9\u30af\u3057\u304b\u5b9f\u884c\u3067\u304d\u306a\u3044\u3088\u3046\u306b\u3057\u305f\u4f8b\u3067\u3059. \u5b9f\u884c\u4f8b\u306f\u4ee5\u4e0b\u306e\u901a\u308a\u3067\u3059. \u6642\u9593\u3092\u898b\u308c\u3070\u5206\u304b\u308b\u901a\u308a, \u521d\u671f\u5024 0 \u306e\u30bb\u30de\u30d5\u30a9\u3092\u5229\u7528\u3059\u308b\u3053\u3068\u3067, \u30bf\u30b9\u30af\u306e\u51e6\u7406\u306e\u5b8c\u4e86\u3092\u5f85\u3064\u3068\u3044\u3046\u4f7f\u3044\u65b9\u304c\u3067\u304d\u307e\u3059. \u30d7\u30ed\u30b0\u30e9\u30e0\u3068\u5b9f\u884c\u4f8b\u306f\u4ee5\u4e0b\u306e\u901a\u308a\u3067\u3059.<\/p>\n \u4ee5\u4e0a\u306e\u5b9f\u884c\u7d50\u679c\u304b\u3089\u3082, \u975e\u540c\u671f\u306b\u5b9f\u884c\u3057\u305f\u30bf\u30b9\u30af\u304c\u5b8c\u4e86\u3059\u308b\u306e\u3092\u5f85\u3063\u3066, \u51e6\u7406\u3092\u7d99\u7d9a\u3059\u308b\u3053\u3068\u304c\u51fa\u6765\u3066\u3044\u308b\u3053\u3068\u304c\u308f\u304b\u308a\u307e\u3059.<\/p>\n \u4e0a\u8a18\u306e1\u3064\u306e\u30bf\u30b9\u30af\u306e\u5b8c\u4e86\u3092\u5f85\u6a5f\u3059\u308b\u4f8b\u3092\u62e1\u5f35\u3059\u308b\u3068, \u8907\u6570\u306e\u30bf\u30b9\u30af\u306e\u5b8c\u4e86\u3092\u5f85\u6a5f\u3059\u308b\u30d7\u30ed\u30b0\u30e9\u30e0\u3082\u30bb\u30de\u30d5\u30a9\u3092\u4f7f\u3063\u3066\u66f8\u304f\u3053\u3068\u304c\u3067\u304d\u307e\u3059. \u305d\u308c\u305e\u308c\u306e\u30bf\u30b9\u30af\u3067 \u4e0a\u8a18\u306e\u4f8b\u306f\u30c7\u30a3\u30b9\u30d1\u30c3\u30c1\u30bb\u30de\u30d5\u30a9\u3067\u306f\u306a\u304f, \u30c7\u30a3\u30b9\u30d1\u30c3\u30c1\u30b0\u30eb\u30fc\u30d7\u3092\u5229\u7528\u3057\u3066\u3082\u5b9f\u88c5\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059. \u4eca\u56de\u306e\u4f8b\u3067\u306f\u3053\u306e\u65b9\u6cd5\u306e\u65b9\u304c\u3088\u308a\u30b7\u30f3\u30d7\u30eb\u304b\u3082\u3057\u308c\u307e\u305b\u3093.<\/p>\n \u30c7\u30a3\u30b9\u30d1\u30c3\u30c1\u30b0\u30eb\u30fc\u30d7\u306f\u4e00\u9023\u306e\u30bf\u30b9\u30af\u3092\u30b0\u30eb\u30fc\u30d7\u5316\u3057\u3066\u304a\u304d, \u305d\u306e\u5168\u3066\u306e\u30bf\u30b9\u30af\u306e\u5b8c\u4e86\u3092\u5f85\u6a5f\u3055\u305b\u3064\u3053\u3068\u304c\u3067\u304d\u307e\u3059. \u30d7\u30ed\u30b0\u30e9\u30e0\u3068\u5b9f\u884c\u4f8b\u306f\u4ee5\u4e0b\u306e\u901a\u308a\u3067\u3059.<\/p>\n macOS\u30fbiOS \u3067\u306f\u30bb\u30de\u30d5\u30a9\u304c GCD \u306b\u30c7\u30a3\u30b9\u30d1\u30c3\u30c1\u30bb\u30de\u30d5\u30a9\u3068\u3057\u3066\u5b9f\u88c5\u3055\u308c\u3066\u304a\u308a, \u7c21\u5358\u306b\u5229\u7528\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059. \u30c7\u30a3\u30b9\u30d1\u30c3\u30c1\u30bb\u30de\u30d5\u30a9\u3092\u4f7f\u3063\u3066\u6709\u9650\u30ea\u30bd\u30fc\u30b9\u3078\u306e\u30a2\u30af\u30bb\u30b9\u5236\u9650\u3084, \u30bf\u30b9\u30af\u306e\u5b8c\u4e86\u5f85\u3061\u306a\u3069\u3092\u5b9f\u88c5\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059. GCD \u306b\u306f\u30bb\u30de\u30d5\u30a9\u3088\u308a\u3082\u9ad8\u7d1a\u306a\u30a4\u30f3\u30bf\u30fc\u30d5\u30a7\u30a4\u30b9\u3082\u63d0\u4f9b\u3055\u308c\u3066\u3044\u308b\u305f\u3081, \u9069\u6750\u9069\u6240\u3067\u4f7f\u3044\u5206\u3051\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059.<\/p>\n Grand Central Dispatch (GCD) \u306f Mac OS X 10.6+ \u3084 iOS 4+ \u3067\u5229\u7528\u51fa\u6765\u308b\u6280\u8853\u3067, \u30bf\u30b9\u30af\u3092\u7c21\u5358\u306b\u975e\u540c\u671f\u3067\u5b9f\u884c\u3055\u305b\u308b\u3053\u3068\u304c\u51fa\u6765\u308b\u6280\u8853\u306e\u4e00\u3064\u3067\u3059. \u3053\u306e\u8a18\u4e8b\u3067\u306f GCD \u306e\u6a5f […]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[26,3],"tags":[82,11,21,60],"_links":{"self":[{"href":"https:\/\/blog.ymyzk.com\/wp-json\/wp\/v2\/posts\/1030"}],"collection":[{"href":"https:\/\/blog.ymyzk.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.ymyzk.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.ymyzk.com\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.ymyzk.com\/wp-json\/wp\/v2\/comments?post=1030"}],"version-history":[{"count":28,"href":"https:\/\/blog.ymyzk.com\/wp-json\/wp\/v2\/posts\/1030\/revisions"}],"predecessor-version":[{"id":1638,"href":"https:\/\/blog.ymyzk.com\/wp-json\/wp\/v2\/posts\/1030\/revisions\/1638"}],"wp:attachment":[{"href":"https:\/\/blog.ymyzk.com\/wp-json\/wp\/v2\/media?parent=1030"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.ymyzk.com\/wp-json\/wp\/v2\/categories?post=1030"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.ymyzk.com\/wp-json\/wp\/v2\/tags?post=1030"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}
\n<\/p>\n\u30c7\u30a3\u30b9\u30d1\u30c3\u30c1\u30bb\u30de\u30d5\u30a9\u3068\u306f<\/h2>\n
\nApple, Inc. \u4e26\u5217\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u30ac\u30a4\u30c9 p.40<\/a><\/cite><\/p><\/blockquote>\ncreate<\/code>,
wait<\/code>,
signal<\/code> \u306e3\u3064\u304c\u3042\u308a\u307e\u3059.<\/p>\n
\n
DispatchSemaphore(value: value)<\/code>\u00a0\u2014 \u521d\u671f\u5024\u3092\u6307\u5b9a\u3057\u3066\u30bb\u30de\u30d5\u30a9\u3092\u4f5c\u6210\u3059\u308b<\/li>\n
semaphore.wait()<\/code> \u2014 \u30bb\u30de\u30d5\u30a9\u306e\u5024\u3092\u30c7\u30af\u30ea\u30e1\u30f3\u30c8\u3059\u308b (P\u64cd\u4f5c)<\/li>\n
semaphore.signal()<\/code>\u00a0\u2014 \u30bb\u30de\u30d5\u30a9\u306e\u5024\u3092\u30a4\u30f3\u30af\u30ea\u30e1\u30f3\u30c8\u3059\u308b (V\u64cd\u4f5c)<\/li>\n<\/ul>\n
\u6709\u9650\u306e\u30ea\u30bd\u30fc\u30b9\u3078\u306e\u30a2\u30af\u30bb\u30b9\u3092\u5236\u9650\u3059\u308b<\/h2>\n
func example1() {\r\n let queue = DispatchQueue.global(qos: .default)\r\n for i in 0..<10 {\r\n queue.async {\r\n NSLog(\"Start: \\(i)\")\r\n print(\"Start: \\(i)\")\r\n sleep(3)\r\n NSLog(\"End: \\(i)\")\r\n }\r\n }\r\n}\r\nexample1()<\/code><\/pre>\n
2016-10-28 23:19:39.595 SemaphoreTest[19679:5314281] Start: 0\r\n2016-10-28 23:19:39.595 SemaphoreTest[19679:5314291] Start: 7\r\n2016-10-28 23:19:39.595 SemaphoreTest[19679:5314289] Start: 5\r\n2016-10-28 23:19:39.595 SemaphoreTest[19679:5314284] Start: 2\r\n2016-10-28 23:19:39.595 SemaphoreTest[19679:5314292] Start: 9\r\n2016-10-28 23:19:39.595 SemaphoreTest[19679:5314290] Start: 6\r\n2016-10-28 23:19:39.595 SemaphoreTest[19679:5314293] Start: 8\r\n2016-10-28 23:19:39.595 SemaphoreTest[19679:5314288] Start: 4\r\n2016-10-28 23:19:39.595 SemaphoreTest[19679:5314287] Start: 3\r\n2016-10-28 23:19:39.595 SemaphoreTest[19679:5314282] Start: 1\r\n2016-10-28 23:19:42.595 SemaphoreTest[19679:5314291] End: 7\r\n2016-10-28 23:19:42.595 SemaphoreTest[19679:5314281] End: 0\r\n2016-10-28 23:19:42.595 SemaphoreTest[19679:5314289] End: 5\r\n2016-10-28 23:19:42.595 SemaphoreTest[19679:5314284] End: 2\r\n2016-10-28 23:19:42.596 SemaphoreTest[19679:5314292] End: 9\r\n2016-10-28 23:19:42.596 SemaphoreTest[19679:5314290] End: 6\r\n2016-10-28 23:19:42.596 SemaphoreTest[19679:5314293] End: 8\r\n2016-10-28 23:19:42.596 SemaphoreTest[19679:5314288] End: 4\r\n2016-10-28 23:19:42.596 SemaphoreTest[19679:5314287] End: 3\r\n2016-10-28 23:19:42.597 SemaphoreTest[19679:5314282] End: 1<\/code><\/pre>\n
NSLog(\"Start: \\(i)\")<\/code> \u304b\u3089
NSLog(\"End: \\(i)\")<\/code> \u306e\u90e8\u5206\u306f\u540c\u6642\u306b2\u500b\u306e\u30bf\u30b9\u30af\u3067\u3057\u304b\u5b9f\u884c\u3055\u308c\u306a\u3044\u3053\u3068\u3092\u4fdd\u8a3c\u3067\u304d\u307e\u3059.<\/p>\n
func semaphoreExample1() {\r\n let semaphore = DispatchSemaphore(value: 2)\r\n let queue = DispatchQueue.global(qos: .default)\r\n for i in 0..<10 {\r\n queue.async {\r\n semaphore.wait()\r\n NSLog(\"Start: \\(i)\")\r\n sleep(3)\r\n NSLog(\"End: \\(i)\")\r\n semaphore.signal()\r\n }\r\n }\r\n}\r\nsemaphoreExample1()\r\n<\/code><\/pre>\n
NSLog(\"Start: \\(i)\")<\/code> \u304b\u3089
NSLog(\"End: \\(i)\")<\/code> \u306e\u90e8\u5206\u304c\u540c\u6642\u306b2\u500b\u3057\u304b\u5b9f\u884c\u3055\u308c\u3066\u3044\u306a\u3044\u3053\u3068\u3092\u78ba\u8a8d\u3067\u304d\u307e\u3059.<\/p>\n
2016-10-28 23:24:15.270 SemaphoreTest[19892:5321690] Start: 0\r\n2016-10-28 23:24:15.270 SemaphoreTest[19892:5321704] Start: 1\r\n2016-10-28 23:24:18.342 SemaphoreTest[19892:5321690] End: 0\r\n2016-10-28 23:24:18.342 SemaphoreTest[19892:5321704] End: 1\r\n2016-10-28 23:24:18.343 SemaphoreTest[19892:5321693] Start: 2\r\n2016-10-28 23:24:18.343 SemaphoreTest[19892:5321691] Start: 3\r\n2016-10-28 23:24:21.370 SemaphoreTest[19892:5321693] End: 2\r\n2016-10-28 23:24:21.370 SemaphoreTest[19892:5321691] End: 3\r\n2016-10-28 23:24:21.370 SemaphoreTest[19892:5321713] Start: 5\r\n2016-10-28 23:24:21.371 SemaphoreTest[19892:5321712] Start: 4\r\n2016-10-28 23:24:24.371 SemaphoreTest[19892:5321713] End: 5\r\n2016-10-28 23:24:24.371 SemaphoreTest[19892:5321712] End: 4\r\n2016-10-28 23:24:24.371 SemaphoreTest[19892:5321715] Start: 7\r\n2016-10-28 23:24:24.371 SemaphoreTest[19892:5321714] Start: 6\r\n2016-10-28 23:24:27.390 SemaphoreTest[19892:5321715] End: 7\r\n2016-10-28 23:24:27.390 SemaphoreTest[19892:5321714] End: 6\r\n2016-10-28 23:24:27.406 SemaphoreTest[19892:5321716] Start: 8\r\n2016-10-28 23:24:27.406 SemaphoreTest[19892:5321717] Start: 9\r\n2016-10-28 23:24:30.443 SemaphoreTest[19892:5321717] End: 9\r\n2016-10-28 23:24:30.443 SemaphoreTest[19892:5321716] End: 8<\/code><\/pre>\n
\u30bf\u30b9\u30af\u306e\u5b8c\u4e86\u3092\u5f85\u6a5f\u3059\u308b<\/h2>\n
func semaphoreExample2() {\r\n let semaphore = DispatchSemaphore(value: 0)\r\n let queue = DispatchQueue.global(qos: .default)\r\n queue.async {\r\n NSLog(\"Running async task...\")\r\n sleep(3)\r\n NSLog(\"Async task completed\")\r\n \/\/ \u30bb\u30de\u30d5\u30a9\u306e\u5024\u3092\u30a4\u30f3\u30af\u30ea\u30e1\u30f3\u30c8\u3059\u308b, \u3064\u307e\u308a\u30bb\u30de\u30d5\u30a9\u306e\u5024\u304c\u6b63\u306b\u306a\u308b\r\n semaphore.signal()\r\n }\r\n NSLog(\"Waiting async task...\")\r\n \/\/ \u30bb\u30de\u30d5\u30a9\u306e\u5024\u304c\u6b63\u306b\u306a\u308b\u306e\u3092\u5f85\u3064\r\n \/\/ \u30bb\u30de\u30d5\u30a9\u306e\u5024\u304c\u6b63\u306b\u306a\u3063\u305f\u3089, \u30c7\u30af\u30ea\u30e1\u30f3\u30c8\u3057\u3066\u9032\u3080\r\n semaphore.wait()\r\n NSLog(\"Continue!\")\r\n}\r\nsemaphoreExample2()<\/code><\/pre>\n
2016-10-29 11:56:14.884 SemaphoreTest[21955:5357365] Waiting async task...\r\n2016-10-29 11:56:14.884 SemaphoreTest[21955:5357413] Running async task...\r\n2016-10-29 11:56:17.887 SemaphoreTest[21955:5357413] Async task completed\r\n2016-10-29 11:56:17.887 SemaphoreTest[21955:5357365] Continue!<\/code><\/pre>\n
\u8907\u6570\u306e\u30bf\u30b9\u30af\u306e\u5b8c\u4e86\u3092\u5f85\u6a5f\u3059\u308b<\/h2>\n
semaphore.signal()<\/code>\u00a0\u3092\u547c\u3073\u51fa\u3057, \u305d\u306e\u6570\u306b\u5bfe\u5fdc\u3059\u308b\u3060\u3051
semaphore.wait()<\/code> \u3092\u547c\u3073\u51fa\u3057\u307e\u3059.<\/p>\n
func semaphoreExample3() {\r\n let semaphore = DispatchSemaphore(value: 0)\r\n let queue = DispatchQueue.global(qos: .default)\r\n let n = 9\r\n for i in 0..<n {\r\n queue.async {\r\n NSLog(\"\\(i): Running async task...\")\r\n sleep(3)\r\n NSLog(\"\\(i): Async task completed\")\r\n semaphore.signal()\r\n }\r\n }\r\n NSLog(\"Waiting async task...\")\r\n for i in 0..<n {\r\n semaphore.wait()\r\n NSLog(\"\\(i + 1)\/\\(n) completed\")\r\n }\r\n NSLog(\"Continue!\")\r\n}\r\nsemaphoreExample3()<\/code><\/pre>\n
2016-10-29 11:58:17.624 SemaphoreTest[22067:5360551] 1: Running async task...\r\n2016-10-29 11:58:17.624 SemaphoreTest[22067:5360586] 3: Running async task...\r\n2016-10-29 11:58:17.624 SemaphoreTest[22067:5360548] 2: Running async task...\r\n2016-10-29 11:58:17.624 SemaphoreTest[22067:5360549] 0: Running async task...\r\n2016-10-29 11:58:17.625 SemaphoreTest[22067:5360587] 4: Running async task...\r\n2016-10-29 11:58:17.625 SemaphoreTest[22067:5360588] 5: Running async task...\r\n2016-10-29 11:58:17.625 SemaphoreTest[22067:5360589] 6: Running async task...\r\n2016-10-29 11:58:17.626 SemaphoreTest[22067:5360590] 7: Running async task...\r\n2016-10-29 11:58:17.624 SemaphoreTest[22067:5360214] Waiting async task...\r\n2016-10-29 11:58:17.626 SemaphoreTest[22067:5360592] 8: Running async task...\r\n2016-10-29 11:58:20.644 SemaphoreTest[22067:5360586] 3: Async task completed\r\n2016-10-29 11:58:20.644 SemaphoreTest[22067:5360551] 1: Async task completed\r\n2016-10-29 11:58:20.644 SemaphoreTest[22067:5360548] 2: Async task completed\r\n2016-10-29 11:58:20.644 SemaphoreTest[22067:5360549] 0: Async task completed\r\n2016-10-29 11:58:20.644 SemaphoreTest[22067:5360587] 4: Async task completed\r\n2016-10-29 11:58:20.644 SemaphoreTest[22067:5360588] 5: Async task completed\r\n2016-10-29 11:58:20.644 SemaphoreTest[22067:5360589] 6: Async task completed\r\n2016-10-29 11:58:20.644 SemaphoreTest[22067:5360590] 7: Async task completed\r\n2016-10-29 11:58:20.644 SemaphoreTest[22067:5360592] 8: Async task completed\r\n2016-10-29 11:58:20.646 SemaphoreTest[22067:5360214] 1\/9 completed\r\n2016-10-29 11:58:20.648 SemaphoreTest[22067:5360214] 2\/9 completed\r\n2016-10-29 11:58:20.649 SemaphoreTest[22067:5360214] 3\/9 completed\r\n2016-10-29 11:58:20.650 SemaphoreTest[22067:5360214] 4\/9 completed\r\n2016-10-29 11:58:20.651 SemaphoreTest[22067:5360214] 5\/9 completed\r\n2016-10-29 11:58:20.651 SemaphoreTest[22067:5360214] 6\/9 completed\r\n2016-10-29 11:58:20.655 SemaphoreTest[22067:5360214] 7\/9 completed\r\n2016-10-29 11:58:20.662 SemaphoreTest[22067:5360214] 8\/9 completed\r\n2016-10-29 11:58:20.663 SemaphoreTest[22067:5360214] 9\/9 completed\r\n2016-10-29 11:58:20.664 SemaphoreTest[22067:5360214] Continue!<\/code><\/pre>\n
\u8907\u6570\u306e\u30bf\u30b9\u30af\u306e\u5b8c\u4e86\u3092\u5f85\u6a5f\u3059\u308b (\u30c7\u30a3\u30b9\u30d1\u30c3\u30c1\u30b0\u30eb\u30fc\u30d7\u3092\u5229\u7528\u3059\u308b\u5834\u5408)<\/h2>\n
func semaphoreExample4() {\r\n let queue = DispatchQueue.global(qos: .default)\r\n let group = DispatchGroup()\r\n let n = 9\r\n for i in 0..<n {\r\n queue.async(group: group) {\r\n NSLog(\"\\(i): Running async task...\")\r\n sleep(3)\r\n NSLog(\"\\(i): Async task completed\")\r\n }\r\n }\r\n NSLog(\"Waiting async task...\")\r\n group.wait()\r\n NSLog(\"Continue!\")\r\n}\r\nsemaphoreExample4()<\/code><\/pre>\n
2016-10-29 12:02:21.135 SemaphoreTest[22218:5365232] 1: Running async task...\r\n2016-10-29 12:02:21.135 SemaphoreTest[22218:5365251] 0: Running async task...\r\n2016-10-29 12:02:21.135 SemaphoreTest[22218:5365231] 3: Running async task...\r\n2016-10-29 12:02:21.135 SemaphoreTest[22218:5365234] 2: Running async task...\r\n2016-10-29 12:02:21.135 SemaphoreTest[22218:5365260] 4: Running async task...\r\n2016-10-29 12:02:21.135 SemaphoreTest[22218:5365261] 5: Running async task...\r\n2016-10-29 12:02:21.135 SemaphoreTest[22218:5365262] 6: Running async task...\r\n2016-10-29 12:02:21.136 SemaphoreTest[22218:5365263] 7: Running async task...\r\n2016-10-29 12:02:21.136 SemaphoreTest[22218:5365264] 8: Running async task...\r\n2016-10-29 12:02:21.135 SemaphoreTest[22218:5365189] Waiting async task...\r\n2016-10-29 12:02:24.135 SemaphoreTest[22218:5365232] 1: Async task completed\r\n2016-10-29 12:02:24.135 SemaphoreTest[22218:5365251] 0: Async task completed\r\n2016-10-29 12:02:24.136 SemaphoreTest[22218:5365231] 3: Async task completed\r\n2016-10-29 12:02:24.136 SemaphoreTest[22218:5365234] 2: Async task completed\r\n2016-10-29 12:02:24.137 SemaphoreTest[22218:5365260] 4: Async task completed\r\n2016-10-29 12:02:24.137 SemaphoreTest[22218:5365261] 5: Async task completed\r\n2016-10-29 12:02:24.137 SemaphoreTest[22218:5365262] 6: Async task completed\r\n2016-10-29 12:02:24.138 SemaphoreTest[22218:5365264] 8: Async task completed\r\n2016-10-29 12:02:24.138 SemaphoreTest[22218:5365263] 7: Async task completed\r\n2016-10-29 12:02:24.139 SemaphoreTest[22218:5365189] Continue!<\/code><\/pre>\n
Swift 3 \u3068 Swift 1\/2\u30fbObjective-C \u306e\u5bfe\u5fdc<\/h2>\n
Swift 3<\/h3>\n
\/\/ Swift 3\r\nlet queue = DispatchQueue.global(qos: .default)\r\nqueue.async { print(\"Hello\") }\r\n\r\nlet semaphore = DispatchSemaphore(value: 1)\r\nsemaphore.wait()\r\nsemaphore.signal()\r\n\r\nlet group = DispatchGroup()\r\nqueue.async(group: group) { print(\"Hi!\") }\r\ngroup.wait()\r\n<\/code><\/pre>\n
Swift 1\/2<\/h3>\n
\/\/ Swift 1\/2\r\nlet queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)\r\ndispatch_async(queue) { print(\"Hello\") }\r\n\r\nlet semaphore = dispatch_semaphore_create(1)\r\ndispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER)\r\ndispatch_semaphore_signal(semaphore)\r\n\r\nlet group = dispatch_group_create()\r\ndispatch_group_async(group, queue) { print(\"Hi!\") }\r\ndispatch_group_wait(group, DISPATCH_TIME_FOREVER)\r\n<\/code><\/pre>\n
Objective-C<\/h3>\n
\/\/ Objective-C\r\ndispatch_queue_t queue = dispatch_get_main_queue();\r\ndispatch_async(queue, ^{ NSLog(@\"Hello\"); });\r\n\r\ndispatch_semaphore_t semaphore = dispatch_semaphore_create(1);\r\ndispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);\r\ndispatch_semaphore_signal(semaphore);\r\n\r\ndispatch_group_t group = dispatch_group_create();\r\ndispatch_group_async(group, queue, ^{ NSLog(@\"Hi!\"); });\r\ndispatch_group_wait(group, DISPATCH_TIME_FOREVER);\r\n<\/code><\/pre>\n
\u307e\u3068\u3081<\/h2>\n
\u53c2\u8003<\/h2>\n
\n
\u66f4\u65b0\u5c65\u6b74<\/h2>\n
\n