Semaphore回顾

移动开发 作者: 2024-08-24 18:30:01
用途 在多线程访问可变变量时,是非线程安全的。可能导致程序崩溃。此时,可以通过使用信号量(semaphore)技术,保证多线程处理某段代码时,后面线程等待前面线程执行,保证了多线程的安全性。使用方法记

用途

不用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);//参数1,代表只能有1个线程同时访问被限制的代码。
    for (int i = 0; i < 1000; ++i) {
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,^{
            dispatch_semaphore_wait(lSema,DISPATCH_TIME_FOREVER);//wait使信号量的值-1,如果结果为0,其他线程来访问时,需要等待。直到信号量的值大于0
            [lArrM addObject:@(i)];
            NSLog(@"lArrM.count:%zd",lArrM.count);
            dispatch_semaphore_signal(lSema);///wait使信号量的值+1,必须和dispatch_semaphore_wait配对使用。
        });
    }
    
    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


...
 SemophoreTest[9092:877715] lArrM.count:999
 SemophoreTest[9092:877730] lArrM.count:1000
 SemophoreTest[9092:877696] final  delayed lArrM.count:1000
原创声明
本站部分文章基于互联网的整理,我们会把真正“有用/优质”的文章整理提供给各位开发者。本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
本文链接:http://www.jiecseo.com/news/show_67962.html
Semaphore回顾