Linked List
Layout
1 |
|
Insert and Delete
Insertion
1 | void Queue<T>::push(const T& value) { |
Delete
1 | T Queue<T>::pop() { |
Now let us consider a long list, and the situation is the reading thread is much moare that write thread
So we need to be more accurate about the granularity of the lock, Then let us redesign the whole class
1 | template <typename T> |
Reimplement the push and pop
1 | void Queue<T>::push(const T& value) { |
Then we addd the condition variable in pthread to concurrent queue.
1 | template <typenmae T> |
The implementation of push
1 | void BlockingQueue <T>::push(const T& value) { |
The implementation of pop, pay attention to the while loop, it is aimed to prevent the fake resume of the process.
1 | T BlockingQueue<T>::pop() { |
Design a size-limited blocking queue
In the previous implementation, the size of the list is infinite. In this implementation, we will set a constant size for the queue.
1 | template <typenmae T> |
Push item into a bounded queue
1 | void BoundedBlockingQueue <T>::push(const T& value ) { |
Pop item out of a bounded queue
1 | T BoundedBlockingQueue<T>::pop() { |