{"id":1439,"date":"2018-11-28T15:01:50","date_gmt":"2018-11-28T07:01:50","guid":{"rendered":"http:\/\/www.yusian.com\/blog\/?p=1439"},"modified":"2020-12-01T12:44:34","modified_gmt":"2020-12-01T04:44:34","slug":"%e6%96%87%e4%bb%b6%e8%af%bb%e5%86%99%e6%93%8d%e4%bd%9c%e4%b8%ad%e9%94%81%e7%9a%84%e4%bd%bf%e7%94%a8","status":"publish","type":"post","link":"https:\/\/www.yusian.com\/blog\/project\/2018\/11\/28\/1501501439.html","title":{"rendered":"\u6587\u4ef6\u8bfb\u5199\u64cd\u4f5c\u4e2d\u9501\u7684\u4f7f\u7528"},"content":{"rendered":"<p>1\u3001\u7b80\u8ff0<br \/>\n1.1\u3001\u591a\u7ebf\u7a0b\u64cd\u4f5c\u4e2d\u5fc5\u7136\u4f1a\u6d89\u53ca\u5230\u9501\u7684\u6982\u5ff5\uff0c\u4f7f\u7528\u9501\u6765\u786e\u4fdd\u7ebf\u7a0b\u5b89\u5168\uff1b<br \/>\n1.2\u3001\u4e8b\u5b9e\u4e0a\u67d0\u4e9b\u64cd\u4f5c\uff0c\u6bd4\u5982\u6570\u636e\u8bfb\u53d6\u662f\u53ef\u4ee5\u5141\u8bb8\u591a\u7ebf\u7a0b\u540c\u65f6\u64cd\u4f5c\u7684\uff1b<br \/>\n1.3\u3001\u4f46\u5199\u64cd\u4f5c\u4e0d\u884c\uff0c\u6570\u636e\u5199\u5165\u52a8\u4f5c\u5fc5\u987b\u662f\u540c\u6b65\u7684\uff0c\u5426\u5219\u4f1a\u51fa\u73b0\u6570\u636e\u9519\u8bef\uff1b<br \/>\n1.4\u3001\u5982\u679c\u8bfb\u3001\u5199\u64cd\u4f5c\u52a8\u4f5c\u540c\u65f6\u53d1\u751f\uff0c\u5e76\u4e14\u591a\u7ebf\u7a0b\u5e76\u53d1\u64cd\u4f5c\u65f6\u8be5\u5982\u4f55\u5904\u7406\uff1f<br \/>\n1.5\u3001\u901a\u8fc7\u666e\u901a\u52a0\u9501\u5b9e\u73b0\u7ebf\u7a0b\u540c\u6b65\uff0c\u8fd9\u6837\u7ebf\u7a0b\u662f\u5b89\u5168\u4e86\uff0c\u4f46\u5f71\u54cd\u4e86\u8bfb\u53d6\u7684\u6548\u7387\uff1b<br \/>\n1.6\u3001\u57fa\u672c\u601d\u8def\u662f<br \/>\n1.6.1\u3001\u5141\u8bb8\u8bfb\u53d6\u64cd\u4f5c\u591a\u7ebf\u7a0b\u5e76\u53d1\u8fdb\u884c\uff1b<br \/>\n1.6.2\u3001\u5199\u5165\u64cd\u4f5c\u53ea\u80fd\u5355\u4e2a\u7ebf\u7a0b\u540c\u6b65\u8fdb\u884c\uff1b<br \/>\n1.7\u3001\u5e38\u7528\u7684\u89e3\u51b3\u65b9\u6848\u6709\u4e24\u4e2a<br \/>\n1.7.1\u3001pthread_rwlock<br \/>\n1.7.2\u3001dispatch_barrier_async<\/p>\n<p>2\u3001\u4ee3\u7801\u793a\u4f8b<br \/>\n2.1\u3001pthread_rwlock<!--more--><\/p>\n<pre lang=\"objc\" line=\"1\">#import \"ViewController.h\"\r\n#import <pthread.h>\r\n@interface ViewController()\r\n{\r\n    pthread_rwlock_t    _rwlock;\r\n}\r\n@end\r\n@implementation ViewController\r\n\r\n- (void)viewDidLoad\r\n{\r\n    [super viewDidLoad];\r\n    pthread_rwlock_init(&_rwlock, NULL);\r\n    for (int i = 0; i < 10; i++) {\r\n        dispatch_async(dispatch_get_global_queue(0, 0), ^{\r\n            [self read];\r\n        });\r\n        dispatch_async(dispatch_get_global_queue(0, 0), ^{\r\n            [self write];\r\n        });\r\n    }\r\n}\r\n\r\n- (void)read\r\n{\r\n    pthread_rwlock_rdlock(&#038;_rwlock);\r\n    sleep(1);\r\n    NSLog(@\"read...\");\r\n    pthread_rwlock_unlock(&#038;_rwlock);\r\n}\r\n\r\n- (void)write\r\n{\r\n    pthread_rwlock_wrlock(&#038;_rwlock);\r\n    sleep(1);\r\n    NSLog(@\"write...\");\r\n    pthread_rwlock_unlock(&#038;_rwlock);\r\n}\r\n@end<\/pre>\n<p>\u6267\u884c\u7ed3\u679c\uff1a<\/p>\n<pre lang=\"mach\">2018-11-28 14:40:42.959452+0800 MultiThread[11616:2172598] read...\r\n2018-11-28 14:40:43.964333+0800 MultiThread[11616:2172597] write...\r\n2018-11-28 14:40:44.969387+0800 MultiThread[11616:2172599] read...\r\n2018-11-28 14:40:45.974642+0800 MultiThread[11616:2172673] write...\r\n2018-11-28 14:40:46.975541+0800 MultiThread[11616:2172674] read...\r\n2018-11-28 14:40:47.979765+0800 MultiThread[11616:2172675] write...\r\n2018-11-28 14:40:48.982189+0800 MultiThread[11616:2172676] read...\r\n2018-11-28 14:40:49.986234+0800 MultiThread[11616:2172677] write...\r\n2018-11-28 14:40:50.990627+0800 MultiThread[11616:2172678] read...\r\n2018-11-28 14:40:51.995698+0800 MultiThread[11616:2172679] write...\r\n2018-11-28 14:40:53.000450+0800 MultiThread[11616:2172680] read...\r\n2018-11-28 14:40:54.004271+0800 MultiThread[11616:2172681] write...\r\n2018-11-28 14:40:55.007637+0800 MultiThread[11616:2172682] read...\r\n2018-11-28 14:40:56.010467+0800 MultiThread[11616:2172683] write...\r\n2018-11-28 14:40:57.016078+0800 MultiThread[11616:2172684] read...\r\n2018-11-28 14:40:58.018431+0800 MultiThread[11616:2172685] write...\r\n2018-11-28 14:40:59.024275+0800 MultiThread[11616:2172687] write...\r\n2018-11-28 14:41:00.029617+0800 MultiThread[11616:2172686] read...\r\n2018-11-28 14:41:00.029645+0800 MultiThread[11616:2172688] read...\r\n2018-11-28 14:41:01.034141+0800 MultiThread[11616:2172689] write...<\/pre>\n<p>2.2\u3001dispatch_barrier_asyn<\/p>\n<pre lang=\"objc\" line=\"1\">#import \"ViewController.h\"\r\n#import <pthread.h>\r\n@interface ViewController()\r\n{\r\n    dispatch_queue_t    _queue;\r\n}\r\n@end\r\n@implementation ViewController\r\n\r\n- (void)viewDidLoad\r\n{\r\n    [super viewDidLoad];\r\n    _queue = dispatch_queue_create(\"rw_queue\", DISPATCH_QUEUE_CONCURRENT);\r\n    for (int i = 0; i < 10; i++) {\r\n        dispatch_async(dispatch_get_global_queue(0, 0), ^{\r\n            [self read];\r\n        });\r\n        dispatch_async(dispatch_get_global_queue(0, 0), ^{\r\n            [self write];\r\n        });\r\n    }\r\n}\r\n\r\n- (void)read\r\n{\r\n    dispatch_async(_queue, ^{\r\n        sleep(1);\r\n        NSLog(@\"read...\");\r\n    });\r\n}\r\n\r\n- (void)write\r\n{\r\n    dispatch_barrier_async(_queue, ^{\r\n        sleep(1);\r\n        NSLog(@\"write...\");\r\n    });\r\n}\r\n@end<\/pre>\n<p>\u6267\u884c\u7ed3\u679c\uff1a<\/p>\n<pre lang=\"mach\">2018-11-28 14:58:53.655607+0800 MultiThread[11850:2327033] read...\r\n2018-11-28 14:58:53.655634+0800 MultiThread[11850:2327049] read...\r\n2018-11-28 14:58:54.658272+0800 MultiThread[11850:2327033] write...\r\n2018-11-28 14:58:55.662524+0800 MultiThread[11850:2327033] write...\r\n2018-11-28 14:58:56.664781+0800 MultiThread[11850:2327033] read...\r\n2018-11-28 14:58:56.664781+0800 MultiThread[11850:2327035] read...\r\n2018-11-28 14:58:56.664781+0800 MultiThread[11850:2327049] read...\r\n2018-11-28 14:58:57.669830+0800 MultiThread[11850:2327049] write...\r\n2018-11-28 14:58:58.674269+0800 MultiThread[11850:2327049] write...\r\n2018-11-28 14:58:59.678279+0800 MultiThread[11850:2327049] write...\r\n2018-11-28 14:59:00.682485+0800 MultiThread[11850:2327049] read...\r\n2018-11-28 14:59:01.687585+0800 MultiThread[11850:2327049] write...\r\n2018-11-28 14:59:02.693012+0800 MultiThread[11850:2327049] read...\r\n2018-11-28 14:59:03.697764+0800 MultiThread[11850:2327049] write...\r\n2018-11-28 14:59:04.702663+0800 MultiThread[11850:2327049] read...\r\n2018-11-28 14:59:05.705765+0800 MultiThread[11850:2327049] write...\r\n2018-11-28 14:59:06.709361+0800 MultiThread[11850:2327049] read...\r\n2018-11-28 14:59:07.710608+0800 MultiThread[11850:2327049] write...\r\n2018-11-28 14:59:08.715003+0800 MultiThread[11850:2327049] read...\r\n2018-11-28 14:59:09.720101+0800 MultiThread[11850:2327049] write...<\/pre>\n<p><strong>3\u3001\u7ed3\u8bba\uff1a\u5728\u6253\u5370read...\u65f6\u53ef\u4ee5\u770b\u5230\u591a\u4e2aread\u8fde\u7eed\u6253\u5370\u65f6\u65f6\u95f4\u57fa\u672c\u4e0a\u662f\u76f8\u540c\u7684\uff0c\u4f46write\u6253\u5370\u662f\u76f8\u96941\u79d2\u7684\uff1b<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>1\u3001\u7b80\u8ff0 1.1\u3001\u591a\u7ebf\u7a0b\u64cd\u4f5c\u4e2d\u5fc5\u7136\u4f1a\u6d89\u53ca\u5230\u9501\u7684\u6982\u5ff5\uff0c\u4f7f\u7528\u9501\u6765\u786e\u4fdd\u7ebf\u7a0b\u5b89\u5168\uff1b 1.2\u3001\u4e8b\u5b9e\u4e0a\u67d0\u4e9b\u64cd\u4f5c\uff0c\u6bd4\u5982\u6570\u636e\u8bfb\u53d6\u662f\u53ef\u4ee5\u5141\u8bb8\u591a\u7ebf\u7a0b\u540c\u65f6\u64cd\u4f5c\u7684\uff1b 1.3\u3001\u4f46\u5199\u64cd\u4f5c\u4e0d\u884c\uff0c\u6570\u636e\u5199\u5165\u52a8\u4f5c\u5fc5\u987b\u662f\u540c\u6b65\u7684\uff0c\u5426\u5219\u4f1a\u51fa\u73b0\u6570\u636e\u9519\u8bef\uff1b 1.4\u3001\u5982\u679c\u8bfb\u3001\u5199\u64cd\u4f5c\u52a8\u4f5c\u540c\u65f6\u53d1\u751f\uff0c\u5e76\u4e14\u591a\u7ebf\u7a0b\u5e76\u53d1\u64cd\u4f5c\u65f6\u8be5\u5982\u4f55\u5904\u7406\uff1f 1.5\u3001\u901a\u8fc7\u666e\u901a\u52a0\u9501\u5b9e\u73b0\u7ebf\u7a0b\u540c\u6b65\uff0c\u8fd9\u6837\u7ebf\u7a0b\u662f\u5b89\u5168\u4e86\uff0c\u4f46\u5f71\u54cd\u4e86\u8bfb\u53d6\u7684\u6548\u7387\uff1b 1.6\u3001\u57fa\u672c\u601d\u8def\u662f 1.6.1\u3001\u5141\u8bb8\u8bfb\u53d6\u64cd\u4f5c\u591a\u7ebf\u7a0b\u5e76\u53d1\u8fdb\u884c\uff1b 1.6.2\u3001\u5199\u5165\u64cd\u4f5c\u53ea\u80fd\u5355\u4e2a\u7ebf\u7a0b\u540c\u6b65\u8fdb\u884c\uff1b 1.7\u3001\u5e38\u7528\u7684\u89e3\u51b3\u65b9\u6848\u6709\u4e24\u4e2a 1.7.1\u3001pthread_rwlock 1.7.2\u3001dispatch_barrier_async 2\u3001\u4ee3\u7801\u793a\u4f8b 2.1\u3001pthread_rwlock<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[88,11],"tags":[77,78,199,192,180,197,198],"class_list":["post-1439","post","type-post","status-publish","format-standard","hentry","category-cpp","category-project","tag-dispatch","tag-gcd","tag-pthread","tag-192","tag-180","tag-197","tag-198"],"_links":{"self":[{"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/posts\/1439","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/comments?post=1439"}],"version-history":[{"count":0,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/posts\/1439\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/media?parent=1439"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/categories?post=1439"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/tags?post=1439"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}