任何单一的消费单生产无锁队列实现用C? [英] Any single-consumer single-producer lock free queue implementation in C?

查看:165
本文介绍了任何单一的消费单生产无锁队列实现用C?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写一个消费者线程与生产者线程的程序,现在看来队列同步程序中的大开销,我找了一些锁免费队列实现,但只找到兰波特的版本和改进版本在PPoPP '08:

I'm writing a program with a consumer thread and a producer thread, now it seems queue synchronization is a big overhead in the program, and I looked for some lock free queue implementations, but only found Lamport's version and an improved version on PPoPP '08:

enqueue_nonblock(data) {
    if (NULL != buffer[head]) {
        return EWOULDBLOCK;
    }
    buffer[head] = data;
    head = NEXT(head);
    return 0;
}

dequeue_nonblock(data) {
    data = buffer[tail];
    if (NULL == data) {
        return EWOULDBLOCK;
    }
    buffer[tail] = NULL;
    tail = NEXT(tail);
    return 0;
}

两个版本都需要对数据的pre-分配的数组,我的问题是,是有它使用的malloc()动态分配空间,任何单一的消费单生产无锁队列实现?

Both versions require a pre-allocated array for the data, my question is that is there any single-consumer single-producer lock-free queue implementation which uses malloc() to allocate space dynamically?

和另外一个相关的问题是,我怎么能衡量队列同步精确的开销?如需要的pthread_mutex_lock()等的多少时间。

And another related question is, how can I measure exact overhead in queue synchronization? Such as how much time it takes of pthread_mutex_lock(), etc.

推荐答案

如果你担心性能,加入的malloc()的组合不会帮助的东西。如果你不担心的表现,为什么不直接控制通过一个互斥访问队列。你有没有实际测量这种实现的性能?这听起来我好像你正在下降的premature优化familar路线。

If you are worried about performance, adding malloc() to the mix won't help things. And if you are not worried about performance, why not simply control access to the queue via a mutex. Have you actually measured the performance of such an implementation? It sounds to me as though you are going down the familar route of premature optimisation.

这篇关于任何单一的消费单生产无锁队列实现用C?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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