0

所以我正在阅读 Facebook 很棒的AsyncDisplayKit源代码。具体来说,我正在阅读ASDealloc2MainObject背后的实现。一件事引起了我的注意。

_AS-objc-internal.h的第 423 到 424 行中,程序员将一些任务分派到主队列中。

        dispatch_barrier_async_f(dispatch_get_main_queue(), self,           \
            _objc_deallocOnMainThreadHelper);                               \

与其他调度屏障函数的情况一样,屏障逻辑dispatch_barrier_async_f()仅在处理自定义并发队列时才有意义。对于全局并发队列和主队列,它的作用就像dispatch_async_f()屏障不起作用一样。

那么为什么在这里使用屏障呢?

4

2 回答 2

2

这对我来说似乎是一个错误。充其量,他们试图发出意图并提醒程序员“嘿,这东西是串行的”,但这似乎很可疑。

于 2014-12-11T15:03:27.890 回答
0

所以我找到了一个可能的解释:如果传入的队列的并发宽度设置为 1,则在后台dispatch_async_f()调用。dispatch_barrier_async_f()

实现dispatch_async_f()看起来像这样:

DISPATCH_NOINLINE
    void
    dispatch_async_f(dispatch_queue_t dq, void *ctxt, dispatch_function_t func)
    {
            dispatch_continuation_t dc;

            // No fastpath/slowpath hint because we simply don't know
            if (dq->dq_width == 1) {
                    return dispatch_barrier_async_f(dq, ctxt, func);
            }

            dc = fastpath(_dispatch_continuation_alloc_cacheonly());
            if (!dc) {
                    return _dispatch_async_f_slow(dq, ctxt, func);
            }

            dc->do_vtable = (void *)DISPATCH_OBJ_ASYNC_BIT;
            dc->dc_func = func;
            dc->dc_ctxt = ctxt;

            // No fastpath/slowpath hint because we simply don't know
            if (dq->do_targetq) {
                    return _dispatch_async_f2(dq, dc);
            }

            _dispatch_queue_push(dq, dc);
    }
于 2014-12-12T05:56:46.643 回答