确保我正确解释了嵌套GCD [英] Making sure I'm explaining nested GCD correctly

查看:122
本文介绍了确保我正确解释了嵌套GCD的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我要使用 dispatch_async 将10个任务放在并发队列上.它们不会阻止下一个任务,而是会按顺序进行处理.我的用户界面反应灵敏.

So I'm putting 10 tasks on a concurrent queue using dispatch_async. They do not block the next task, and gets processed in order. My UI is responsive.

for (int i = 0; i < 10; i++) {

    dispatch_async(concurrencyQueue, ^() {

        NSLog(@"..calling insertion method to insert record %d", i);

           dispatch_sync(serialQueue, ^() {

            //this is to simulate writing to database
            NSLog(@"----------START %d---------", i);
            [NSThread sleepForTimeInterval:1.0f];
            NSLog(@"--------FINISHED %d--------", i);

        });

    });
}

在每个任务中,我们通过 dispatch_sync 串行队列上模拟"1秒钟睡眠"的数据库写操作.

Within each task, we simulate a write to database with a "1 sec sleep" on a serial Queue via dispatch_sync.

我一直认为dispatch_sync会阻止所有人,并同步其任务,因为这就是我单独使用它时的行为.但是,在这种情况下,它不会阻塞主线程.相反,它像我想要的那样在后台漂亮地运行.

I always thought dispatch_sync blocks everyone, and syncs its tasks because that's how it behaves when I use it individually. However, in this situation, it does not block the main thread. Instead, it runs beautifully in the background like I want it.

是因为与队列相关联的任何线程都受到影响了吗?

Is it because whatever thread is associated with the queue is being affected?

例如,主线程正在通过dispatch_async执行并发队列,这就是为什么它没有被阻塞的原因.

For example, the main thread is executing the concurrent queue via dispatch_async and that's why it is not blocked.

dispatch_sync仅针对在并发队列上工作的后台线程进行同步和阻止.因此,dispatch_sync与后台线程相关联,因此从不影响我的UI主线程.

The dispatch_sync only syncs and blocks against the background thread that's working on the concurrent queue. Hence, the dispatch_sync is associated with the background thread, thus never affecting my UI main thread.

我的想法正确吗?

谢谢!

推荐答案

您永远不会阻塞主线程,因为您的代码正在concurrencyQueue的线程或serialQueue的线程上运行.这些都不是主线程.

You never block the main thread because your code is running on either the threads of the concurrencyQueue or the thread of the serialQueue. None of those are the main thread.

sleep的所有调用都在serialQueue的线程上一对一地发生.因此,serialQueue的线程被阻止了.

All of the calls to sleep happen one by one on the thread of the serialQueue. So it is the thread of the serialQueue that is blocked.

但是,由于使用dispatch_sync分派给serialQueue,因此也会阻塞并发队列的每个线程.如果在调用dispatch_sync之后添加另一个NSLog,则效果更好.

However, since you dispatch to the serialQueue using dispatch_sync, you are also blocking each thread of the concurrent queue. This would be better pictured if you add another NSLog after the call to dispatch_sync.

for (int i = 0; i < 10; i++) {
    dispatch_async(concurrencyQueue, ^() {
        NSLog(@"..calling insertion method to insert record %d", i);

        dispatch_sync(serialQueue, ^() {
            //this is to simulate writing to database
            NSLog(@"----------START %d---------", i);
            [NSThread sleepForTimeInterval:1.0f];
            NSLog(@"--------FINISHED %d--------", i);
        });

        NSLog(@"..called insertion method to insert record %d", i);
    });
}

dispatch_sync之后的第二个NSLog将更好地显示dispatch_sync如何影响对dispatch_async的调用.

That 2nd NSLog after the dispatch_sync will show you better how the dispatch_sync is affecting the calls to dispatch_async.

这篇关于确保我正确解释了嵌套GCD的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆