多线程GCD

文件1:ViewController.m

#import "ViewController.h"
int mumber = 0;
@interface ViewController ()
{
    dispatch_queue_t queue;
    NSLock *lock;
}
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    queue = dispatch_queue_create("xxx", DISPATCH_QUEUE_SERIAL);
    lock = [[NSLock alloc] init];
    
    //Main queue,提交到Main queue的队列会在主线程中执行,是串行队列
    dispatch_queue_t mainQueue = dispatch_get_main_queue();
    dispatch_async(mainQueue, ^{
        for (int i = 0; i<100; i++) {
            NSLog(@"%@:%i",[NSThread isMainThread]?@"Main thread":@"Sub thread",i);
        }
        NSLog(@"+++");
    });
    
    //死锁  这是循环等待造成的
//    NSLog(@"---------");
//    dispatch_sync(mainQueue, ^{
//        NSLog(@"dispatch_sync");
//    });
    
    dispatch_queue_t queue1 = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_async(queue1, ^{
        NSLog(@"%@",[NSThread isMainThread]?@"Main":@"Sub");
    });
    dispatch_sync(queue1, ^{
        NSLog(@"%@",[NSThread isMainThread]?@"Main":@"Sub");
        dispatch_async(dispatch_get_main_queue(), ^{
            self.view.backgroundColor = [UIColor blackColor];
        });
    });
    
    dispatch_queue_t queue2 = dispatch_queue_create("yyy", DISPATCH_QUEUE_CONCURRENT);
    dispatch_async(queue2, ^{
        for (int i = 0; i<100; i++) {
            [self setFlag:1];
        }
    });
    dispatch_async(queue2, ^{
        for (int i = 0; i<100; i++) {
            [self setFlag:2];
        }
    });
    //提交重复任务
    dispatch_async(queue2, ^{
        for (int i = 0; i<100; i++) {
            [self setFlag:3];
        }
    });
    dispatch_apply(3, queue2, ^(size_t iteraction){
        NSLog(@"%zu",iteraction);
    });
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)setFlag:(int)value
{
    //加锁
    //    @synchronized(self) {
    //        //临界区
    //        num ++;
    //        NSLog(@"serial %d: %d", value, num);
    //    }
    
    //线程间同步
    //    dispatch_async(queue3, ^{
    //        num ++;
    //        NSLog(@"serial %d: %d", value, num);
    //    });
    
    //
    
    [lock lock];
    mumber ++;
    NSLog(@"serial %d: %d", value, mumber);
    [lock unlock];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end


文件2:MySingleton.h

#import <Foundation/Foundation.h>

@interface MySingleton : NSObject
+ (instancetype)instance;
@end




文件3:MySingleton.m

#import "MySingleton.h"

@implementation MySingleton
+ (instancetype)instance
{
    static MySingleton *instance = nil;
    static dispatch_once_t onceToke ;
    dispatch_once(&onceToke,^{
        instance = [[MySingleton alloc] init];
    });
    return instance;
}
@end

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值