Semaphore回顾
Posted dast1
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Semaphore回顾相关的知识,希望对你有一定的参考价值。
用途
多线程访问可变变量时,是非线程安全的。可能导致程序崩溃。此时,可以通过使用信号量(semaphore)技术,保证多线程处理某段代码时,后面线程等待前面线程执行,保证了多线程的安全性。使用方法记两个就行了,一个是wait(dispatch_semaphore_wait),一个是signal(dispatch_semaphore_signal)。根据dispatch_semaphore_wait的参数类型提示去创建信号量(dispatch_semaphore_t)即可。
感觉原理跟PV操作如出一辙。
不用semaphore时code:
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[])
@autoreleasepool
// insert code here...
NSLog(@"Hello, World!");
NSMutableArray *lArrM = [NSMutableArray array];
for (int i = 0; i < 100; ++i)
dispatch_async(dispatch_get_global_queue(0, 0), ^
[lArrM addObject:@(i)];
);
return 0;
运行结果
SemephoreTest[3665:130800] Hello, World!
SemephoreTest(3665,0x700001daa000) malloc: *** error for object 0x10077b2e0: pointer being freed was not allocated
SemephoreTest(3665,0x700001daa000) malloc: *** set a breakpoint in malloc_error_break to debug
SemephoreTest(3665,0x700001e2d000) malloc: *** error for object 0x102b00340: pointer being freed was not allocated
SemephoreTest(3665,0x700001e2d000) malloc: *** set a breakpoint in malloc_error_break to debug
SemephoreTest[3665:130800] lArrM.count:0
(lldb)
用semaphore时code:
//
// ViewController.m
// SemophoreTest
//
// Created by LongMa on 2019/8/22.
// Copyright ? 2019 . All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
[super viewDidLoad];
NSMutableArray *lArrM = [NSMutableArray array];
dispatch_semaphore_t lSema = dispatch_semaphore_create(1);
for (int i = 0; i < 1000; ++i)
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^
dispatch_semaphore_wait(lSema, DISPATCH_TIME_FOREVER);
[lArrM addObject:@(i)];
NSLog(@"lArrM.count:%zd",lArrM.count);
dispatch_semaphore_signal(lSema);
);
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^
NSLog(@"final delayed lArrM.count:%zd",lArrM.count);
);
@end
运行结果(用commandlineTool运行代码时,log存在不完整问题):
...
SemophoreTest[9092:877715] lArrM.count:999
SemophoreTest[9092:877730] lArrM.count:1000
SemophoreTest[9092:877696] final delayed lArrM.count:1000
以上是关于Semaphore回顾的主要内容,如果未能解决你的问题,请参考以下文章
第241天学习打卡(知识点回顾 AQS之Semaphore)