循环队列可以实现一个生产者与一个消费者模型, 不使用锁。
循环队列:容量大小为10
生产者线程:把从0递增的整数依次push到队列,队列满了就等1s(尾部不存储,也就是最大存9个数)。
消费者线程:每3s从队列pop一个元素。
[xiongli@localhost data_struct]$ ./a.out
push 0 at data[0]
push 1 at data[1]
push 2 at data[2]
push 3 at data[3]
push 4 at data[4]
push 5 at data[5]
push 6 at data[6]
push 7 at data[7]
push 8 at data[8]
the queue is full!
the queue is full!
the queue is full!
pop 0 at data[0]
push 9 at data[9]
the queue is full!
the queue is full!
the queue is full!
pop 1 at data[1]
push 10 at data[0]
the queue is full!
the queue is full!
the queue is full!
pull 2 at data[2]
push 11 at data[1]
the queue is full!
the queue is full!
the queue is full!