0
0
0
1. 云栖社区>
2. 博客>
3. 正文

## 深入理解并行编程-分割和同步设计（二）

1.1. 右手锁和左手锁

1.2. 复合双端队列

1.3. 哈希双端队列

``` struct pdeq {

spinlock_t llock;

int lidx;

spinlock_t rlock;

int ridx;

struct deq bkt[DEQ_N_BKTS];

};
```

``` struct element *pdeq_dequeue_l(struct pdeq *d)
{
struct element *e;
int i;
spin_lock(&d->llock);
i = moveright(d->lidx);
e = deq_dequeue_l(&d->bkt[i]);
if (e != NULL)
d->lidx = i;
spin_unlock(&d->llock);
return e;
}

void pdeq_enqueue_l(struct element *e, struct pdeq *d)
{
int i;
spin_lock(&d->llock);
i = d->lidx;
deq_enqueue_l(e, &d->bkt[i]);
d->lidx = moveleft(d->lidx);
spin_unlock(&d->llock);
}

struct element *pdeq_dequeue_r(struct pdeq *d)
{  struct element *e;
int i;
spin_lock(&d->rlock);
i = moveleft(d->ridx);
e = deq_dequeue_r(&d->bkt[i]);
if (e != NULL)
d->ridx = i;
spin_unlock(&d->rlock);
return e;
}

void pdeq_enqueue_r(struct element *e, struct pdeq *d)
{
int i;
spin_lock(&d->rlock);
i = d->ridx;
deq_enqueue_r(e, &d->bkt[i]);
d->ridx = moveright(d->lidx);
spin_unlock(&d->rlock);
}
```

1.4. 再次回到复合双端队列

``` struct list_head *pdeq_dequeue_l(struct pdeq *d)

{

int i;

spin_lock(&d->llock);

e = deq_dequeue_l(&d->ldeq);

if (e == NULL) {

spin_lock(&d->rlock);

e = deq_dequeue_l(&d->rdeq);

list_splice_init(&d->rdeq.chain, &d->ldeq.chain);

spin_unlock(&d->rlock);

}

spin_unlock(&d->llock);

return e;

}

{

int i;

spin_lock(&d->rlock);

e = deq_dequeue_r(&d->rdeq);

if (e == NULL) {

spin_unlock(&d->rlock);

spin_lock(&d->llock);

spin_lock(&d->rlock);

e = deq_dequeue_r(&d->rdeq);

if (e == NULL) {

e = deq_dequeue_r(&d->ldeq);

list_splice_init(&d->ldeq.chain, &d->rdeq.chain);

}

spin_unlock(&d->llock);

}

spin_unlock(&d->rlock);

return e;

}

void pdeq_enqueue_l(struct list_head *e, struct pdeq *d)

{

int i;

spin_lock(&d->llock);

deq_enqueue_l(e, &d->ldeq);

spin_unlock(&d->llock);

}

void pdeq_enqueue_r(struct list_head *e, struct pdeq *d)

{

int i;

spin_lock(&d->rlock);

deq_enqueue_r(e, &d->rdeq);

spin_unlock(&d->rlock);

}

```

1.5. 关于双端队列的讨论

1.6. 关于分割问题示例的讨论

+ 关注