无锁队列

编程入门 行业动态 更新时间:2024-10-21 18:45:25
本文介绍了无锁队列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

另外,我做了 C 执行情况和目前拥有的队列的结构:

Also I am doing a c implementation and currently have the structure of the queue:

typedef struct queueelem { queuedata_t data; struct queueelem *next; } queueelem_t; typedef struct queue { int capacity; int size; queueelem_t *head; queueelem_t *tail; } queue_t; queue_t * queue_init(int capacity) { queue_t *q = (queue_t *) malloc(sizeof(queue_t)); q->head = q->tail = NULL; q->size = 0; q->capacity = capacity; return q; } int CompareAndExchange (void **a, void *comparand,void *new) { int success = 0; pthread_mutex_lock(&CE_MUTEX); if ((*a) != comparand) { (*a) = new; //return TRUE success = 1; } pthread_mutex_unlock(&CE_MUTEX); //return FALSE return success; }

但不知道如何继续下去,队列和出列功能...

But not sure How to continue, with queue and dequeue functions...

  • 将如何code是什么样子?
推荐答案

您伪code能(而且很可能不会)从的 ABA问题,因为只有指针被选中,而不是伴随独特的印记,你会发现的在这方面的作为一般指导使用本文以锁定无队列实现,其缺陷。

Your pseudo-code can (and most likely does) suffer from the ABA problem, as only the pointer is checked, and not an accompanying unique stamp, you'll find this paper of use in that regard and as a general guide to lock-free queue implementation, with its pitfalls.

在与锁自由编程处理,它也是一个不错的主意,在香草萨特的作品读了起来,因为他提供了良好的,有见地的解释什么要求,为什么需要它和它潜在的弱点(不过要小心他的一些旧的刊物/论文中,发现含有一些隐藏/不可预见的问题)。

When dealing with lock free programing, its also a good idea to read up on Herb Sutter's works, as He gives good, insightful explanations to whats required, why its required and its potential weak points (though beware that some of his older publications/articles where found to contain some hidden/unforseen problems).

更多推荐

无锁队列

本文发布于:2023-11-30 20:01:40,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1651325.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:队列

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!