[英]Does an optimistic lock-free FIFO queue implementation exist?
Herb Sutter covered just such a queue as part of his Effective Concurency column in Dr. Dobbs Journal. Herb Sutter在Dobbs Journal博士的有效Concurency专栏中列出了这样一个队列。
I want to conclude the answer given by greyfade , which is based on http://www.drdobbs.com/high-performance-computing/212201163 (the last part of the article), the optimized code would be (with some modification to suit my naming and coding convention) : ` 我想总结一下greyfade给出的答案,它基于http://www.drdobbs.com/high-performance-computing/212201163 (本文的最后一部分),优化的代码将是(通过一些修改)适合我的命名和编码惯例) :`
template <typename T> class LFQueue {
private:
struct LFQNode {
LFQNode( T* val ) : value(val), next(nullptr) { }
T* value;
AtomicPtr<LFQNode> next;
char pad[CACHE_LINE_SIZE - sizeof(T*) - sizeof(AtomicPtr<LFQNode>)];
};
char pad0[CACHE_LINE_SIZE];
LFQNode* first; // for one consumer at a time
char pad1[CACHE_LINE_SIZE - sizeof(LFQNode*)];
InterlockedFlag consumerLock; // shared among consumers
char pad2[CACHE_LINE_SIZE - sizeof(InterlockedFlag)];
LFQNode* last; // for one producer at a time
char pad3[CACHE_LINE_SIZE - sizeof(LFQNode*)];
InterlockedFlag producerLock; // shared among producers
char pad4[CACHE_LINE_SIZE - sizeof(InterlockedFlag)];
public:
LFQueue() {
first = last = new LFQNode( nullptr ); // no more divider
producerLock = consumerLock = false;
}
~LFQueue() {
while( first != nullptr ) {
LFQNode* tmp = first;
first = tmp->next;
delete tmp;
}
}
bool pop( T& result ) {
while( consumerLock.set(true) )
{ } // acquire exclusivity
if( first->next != nullptr ) { // if queue is nonempty
LFQNode* oldFirst = first;
first = first->next;
T* value = first->value; // take it out
first->value = nullptr; // of the Node
consumerLock = false; // release exclusivity
result = *value; // now copy it back
delete value; // and clean up
delete oldFirst; // both allocations
return true; // and report success
}
consumerLock = false; // release exclusivity
return false; // queue was empty
}
bool push( const T& t ) {
LFQNode* tmp = new LFQNode( t ); // do work off to the side
while( producerLock.set(true) )
{ } // acquire exclusivity
last->next = tmp; // A: publish the new item
last = tmp; // B: not "last->next"
producerLock = false; // release exclusivity
return true;
}
};
` `
another question is how do you define CACHE_LINE_SIZE? 另一个问题是你如何定义CACHE_LINE_SIZE? its vary on ever CPUs right? 它的CPU有所不同吗?
Here is my implementation of a lock-free FIFO. 这是我实现的无锁FIFO。
Make sure each item of T is a multiple of 64 bytes (the cache line size in the Intel CPUs) to avoid false sharing. 确保T的每个项目是64字节的倍数(Intel CPU中的缓存行大小),以避免错误共享。
This code compiles with gcc/mingw and should compile with clang. 这段代码用gcc / mingw编译,应该用clang编译。 It's optimized for 64-bit, so to get it to run on 32-bit would need some refactoring. 它针对64位进行了优化,因此要使其在32位上运行需要进行一些重构。
https://github.com/vovoid/vsxu/blob/master/engine/include/vsx_fifo.h https://github.com/vovoid/vsxu/blob/master/engine/include/vsx_fifo.h
vsx_fifo<my_struct, 512> my_fifo;
Sender: 发件人:
my_struct my_struct_inst;
... fill it out ...
while (!my_fifo.produce(my_struct_inst)) {}
Receiver: 接收器:
my_struct my_struct_recv;
while(my_fifo.consume(my_struct_recv))
{
...do stuff...
}
If you're looking for a good lock free queue implementation both Microsoft Visual Studio 2010 & Intel's Thread Building Blocks contain a good LF queue which is similar to the paper. 如果您正在寻找一个良好的无锁队列实现,Microsoft Visual Studio 2010和英特尔的线程构建块都包含一个很好的LF队列,类似于本文。
How about this lfqueue 这个lfqueue怎么样?
This is cross-platform, unlimited enqueue thread safety queue, have been tested multi deq, multi enq-deq and multi enq . 这是跨平台,无限排队的线程安全队列,已经过多deq,多enq-deq和多enq的测试 。 Guarantee memory safe. 保证内存安全。
For example 例如
int* int_data;
lfqueue_t my_queue;
if (lfqueue_init(&my_queue) == -1)
return -1;
/** Wrap This scope in other threads **/
int_data = (int*) malloc(sizeof(int));
assert(int_data != NULL);
*int_data = i++;
/*Enqueue*/
while (lfqueue_enq(&my_queue, int_data) == -1) {
printf("ENQ Full ?\n");
}
/** Wrap This scope in other threads **/
/*Dequeue*/
while ( (int_data = lfqueue_deq(&my_queue)) == NULL) {
printf("DEQ EMPTY ..\n");
}
// printf("%d\n", *(int*) int_data );
free(int_data);
/** End **/
lfqueue_destroy(&my_queue);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.