Chase Lev 双端围栏

ov3*_*337 5 c++ containers multithreading atomic lock-free

我读了文章“正确且高效的弱内存模型的工作窃取”,其中作者给出了这段代码:

int take(Deque *q) {
    size_t b = load_explicit(&q->bottom, relaxed) - 1;
    Array *a = load_explicit(&q->array, relaxed);
    store_explicit(&q->bottom, b, relaxed);
    thread_fence(seq_cst);

    size_t t = load_explicit(&q->top, relaxed);
    int x;
    if (t <= b) {
        /* Non-empty queue. */
        x = load_explicit(&a->buffer[b % a->size], relaxed);
        if (t == b) {
            /* Single last element in queue. */
            if (!compare_exchange_strong_explicit(&q->top, &t, t + 1, seq_cst, relaxed))
                /* Failed race. */

                x = EMPTY;
            store_explicit(&q->bottom, b + 1, relaxed);
        }
    } else { /* Empty queue. */
        x = EMPTY;
        store_explicit(&q->bottom, b + 1, relaxed);
    }
    return x;
}

void push(Deque *q, int x) {
    size_t b = load_explicit(&q->bottom, relaxed);
    size_t t = load_explicit(&q->top, acquire);
    Array *a = load_explicit(&q->array, relaxed);
    if (b - t > a->size - 1) { /* Full queue. */
        resize(q);
        a = load_explicit(&q->array, relaxed);
    }
    store_explicit(&a->buffer[b % a->size], x, relaxed);
    thread_fence(release);
    store_explicit(&q->bottom, b + 1, relaxed);
}

int steal(Deque *q) {
    size_t t = load_explicit(&q->top, acquire);
    thread_fence(seq_cst);
    size_t b = load_explicit(&q->bottom, acquire);
    int x = EMPTY;
    if (t < b) {
        /* Non-empty queue. */
        Array *a = load_explicit(&q->array, consume);
        x = load_explicit(&a->buffer[t % a->size], relaxed);
        if (!compare_exchange_strong_explicit(&q->top, &t, t + 1, seq_cst, relaxed))
        /* Failed race. */
            return ABORT;
    }
    return x;
}
Run Code Online (Sandbox Code Playgroud)

正如我们所看到的,在偷窃行动中他们使用了seq_cst栅栏。此外,他们在行动中也使用了这个围栏。我有两个问题:

  • 为什么我们在窃取操作中需要这个栅栏:您能否解释一些情况的逐步示例,在这种情况下,如果在窃取操作中没有这个栅栏,我们可能会得到未定义的行为或竞争条件?
  • 为什么我们不能仅通过释放内存存储(或栅栏)操作(到底部变量,又名b)来替换取操作中的栅栏?