V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
iOS 开发实用技术导航
NSHipster 中文版
http://nshipster.cn/
cocos2d 开源 2D 游戏引擎
http://www.cocos2d-iphone.org/
CocoaPods
http://cocoapods.org/
Google Analytics for Mobile 统计解决方案
http://code.google.com/mobile/analytics/
WWDC
https://developer.apple.com/wwdc/
Design Guides and Resources
https://developer.apple.com/design/
Transcripts of WWDC sessions
http://asciiwwdc.com
Cocoa with Love
http://cocoawithlove.com/
Cocoa Dev Central
http://cocoadevcentral.com/
NSHipster
http://nshipster.com/
Style Guides
Google Objective-C Style Guide
NYTimes Objective-C Style Guide
Useful Tools and Services
Charles Web Debugging Proxy
Smore
miki6180
V2EX  ›  iDev

关于 GCD 的一点疑惑

  •  
  •   miki6180 · Jun 27, 2015 · 5620 views
    This topic created in 3959 days ago, the information mentioned may be changed or developed.

    既然GCD中使用dispatch_sync()是不会开启线程的, 那么有什么意义呢?是不是同步对于线程是没有意义的?异步才是有意义的?

    26 replies    2015-06-29 16:30:08 +08:00
    fogisland
        1
    fogisland  
       Jun 27, 2015 via iPhone
    有些时候必须要把一些动作交给另外一个线程去做,同时当前线程要等待结果返回才能继续往下走。这时候就需要dispatch_sync。多线程不一定非得异步才有意义啊。
    yahoo21cn
        2
    yahoo21cn  
       Jun 27, 2015   ❤️ 2
    你把GCD关掉,换GMD的协程模式试试
    miki6180
        3
    miki6180  
    OP
       Jun 27, 2015
    @fogisland 那不就意味这是多条线程的同步, 但是我这样写打印出来的线程的number值是一样的, 也就是说是在同一条线程。
    - (void)viewDidLoad {
    [super viewDidLoad];

    dispatch_sync(dispatch_queue_create("cn.lovegh", DISPATCH_QUEUE_PRIORITY_DEFAULT),
    ^{
    NSLog(@"%@", [NSThread currentThread]);
    }
    );


    }。然道它的意思是阻塞当前线程, 那岂不是还要自己开线程, 在哪个线程中再用dispatch_sync()...
    miki6180
        4
    miki6180  
    OP
       Jun 27, 2015
    @fogisland 代码写错,是这段
    - (void)viewDidLoad {
    [super viewDidLoad];

    dispatch_sync(dispatch_queue_create("cn.lovegh", DISPATCH_QUEUE_PRIORITY_DEFAULT),
    ^{
    NSLog(@"%@", [NSThread currentThread]);
    }
    );
    xhacker
        5
    xhacker  
       Jun 27, 2015   ❤️ 1
    dispatch_sync 确实基本用不到。只有一个场景,就是你可以用 dispatch queue 代替锁。参见 http://stackoverflow.com/questions/4607125/using-dispatch-sync-in-grand-central-dispatch
    miki6180
        6
    miki6180  
    OP
       Jun 27, 2015
    @xhacker get~
    fogisland
        7
    fogisland  
       Jun 27, 2015 via iPhone
    @anonysecurity gcd是对线程池的封装,泥这样写,系统很可能就会把新建的gcd分配在当前线程了,因为这样已经满足你的需求了。你如果自己开一个线程,在里面dispatch sync到main queue,就能看到线程是不一样的了。手机打字说不清楚,建议你去了解下gcd和线程的关系就都明白了
    miki6180
        8
    miki6180  
    OP
       Jun 27, 2015
    @fogisland 嗯, 但是有一点是没错, 它不会新开线程。我看了stackoverflow的大概明白了, 就是用来代替锁, 实现多线程的同步~~~生产者消费者的一种模型~。
    66450146
        9
    66450146  
       Jun 27, 2015
    一个简单的场景是,把 CoreData 放在了另一个 queue 上运行,操作 CoreData 之后要根据更新后的内容刷新 view
    elgoog
        10
    elgoog  
       Jun 27, 2015
    本帖有一层楼被删了
    vvqqdd
        11
    vvqqdd  
       Jun 27, 2015
    @yahoo21cn 也可以换一换MJD
    leozhang
        12
    leozhang  
       Jun 27, 2015 via Android
    @elgoog
    哈哈,你也注意到啦。
    miki6180
        13
    miki6180  
    OP
       Jun 27, 2015
    @leozhang 我都没注意到。。。
    Marfal
        14
    Marfal  
       Jun 27, 2015
    我还以为是gong、c、dang。。。
    HFX3389
        15
    HFX3389  
       Jun 27, 2015
    @elgoog 的确是有楼被删除了。。。
    nyanyh
        16
    nyanyh  
       Jun 27, 2015
    @HFX3389 好奇是怎么发现的?左上角的回复数和下面的楼层号是匹配的啊
    loading
        17
    loading  
       Jun 27, 2015 via Android
    @a33004407 我也是…
    ugvfpdcuwfnh
        18
    ugvfpdcuwfnh  
       Jun 27, 2015
    @elgoog
    怎么发现的(⊙_⊙?)
    flowfire
        19
    flowfire  
       Jun 28, 2015
    .............这标题。。。
    freed
        20
    freed  
       Jun 28, 2015
    不止一层..被删了好几层...


    因为帖子刚发没多久我看了 本来上面有好几层说以为是....... 的回复的....
    blessme
        21
    blessme  
       Jun 28, 2015
    这标题要小心被系统抓取导致拔网线。
    HFX3389
        22
    HFX3389  
       Jun 28, 2015
    @nyanyh
    @ugvfpdcuwfnh
    之前这个帖子刚出来的时候我就看到了,,二楼的回复和原来的不同,所以是原来的二三楼都被删除了。
    tao1991123
        23
    tao1991123  
       Jun 28, 2015
    看到GCD还是以为共chan党的代称,还在想 v2ex啥时候变政治论坛了,进来一看。我想多了
    xieyudi1990
        24
    xieyudi1990  
       Jun 29, 2015
    第一个想到的是共残党. 第二个想到的是最大公约数.
    rekey
        25
    rekey  
       Jun 29, 2015
    看标题吓尿了。
    acgeo
        26
    acgeo  
       Jun 29, 2015

    @rekey 真的吓屎了................................
















    md 公然叫嚣伟大滴裆吗!!
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   3070 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 88ms · UTC 14:22 · PVG 22:22 · LAX 07:22 · JFK 10:22
    ♥ Do have faith in what you're doing.