3
所以我的Enqueue和Dequeue函数如下。我如何采取我所拥有的并使其线程安全?我想过使用Windows.h中的互斥锁,但如果可能的话,我不想将我的程序限制为仅限Windows。如何使线程安全的循环队列?
void Queue::Enqueue(int num){
//increase recorded size
size++;
//stick in num
numbers[nextSpace] = num;
//find the next available space
nextSpace = (++nextSpace) % maxSize;
}
int Queue::Dequeue(){
int temp;
temp = items[curSpace];
curSpace = (++curSpace) % maxSize;
size--;
return temp;
}
为什么另起炉灶?使用Boost的'lockfree :: queue'或类似的东西。 (或'spsc_queue',相反,如果你想要一个消费者和一个生产者的循环缓冲区。) –
Boost有一个可移植的同步库。请参阅http://www.boost.org/doc/libs/1_53_0/doc/html/thread/synchronization.html –
您需要什么版本的“线程安全”? STL是线程安全的,你知道吗? –