A concurrent queue based on condition_variables and can be accessed by multi-producers and multi-consumers simultaneously.
More...
#include <concurrent_queue.h>
|
void | SetLimit (size_t limit) |
| Set queue size. When queue_.size() == size_limit_, Putting entities will be blocked. More...
|
|
void | DecProducerNum () |
| When a producer finished producing, it will call this function. When all producers finished producing, blocked consumer will be notified to return.
|
|
void | SetProducerNum (int pn) |
| Set the number of producers to this queue. More...
|
|
void | Put (const T &item) |
| Put an entity into this queue. More...
|
|
void | Put (T &&item) |
| Put an entity into this queue. More...
|
|
bool | Get (T &item) |
| Get an entity from this queue. More...
|
|
bool | TryGetAll (std::deque< T > &items) |
|
size_t | Size () const |
|
|
std::deque< T > | queue_ |
|
size_t | size_limit_ |
|
std::mutex | lock_ |
|
std::condition_variable | empty_ |
|
std::condition_variable | full_ |
|
std::atomic< int > | producer_num_ |
|
template<typename T>
class grape::BlockingQueue< T >
A concurrent queue based on condition_variables and can be accessed by multi-producers and multi-consumers simultaneously.
- Template Parameters
-
T | Type of entities in the queue. |
◆ Get()
Get an entity from this queue.
This function will be blocked when there are alive producers and the queue is empty, and will be waken when entities are put into the queue or all producers finished putting data.
- Parameters
-
item | Reference of an entity to hold the got data. |
- Returns
- If got data, return true. Otherwise, return false.
◆ Put() [1/2]
Put an entity into this queue.
This function will be blocked when the queue is full, that is, queue_.size() == size_limit_.
- Parameters
-
item | The entity to be put. |
◆ Put() [2/2]
Put an entity into this queue.
This function will be blocked when the queue is full, that is, queue_.size() == size_limit_.
- Parameters
-
item | The entity to be put. |
◆ SetLimit()
Set queue size. When queue_.size() == size_limit_, Putting entities will be blocked.
- Parameters
-
limit | Size limit of the queue. |
◆ SetProducerNum()
Set the number of producers to this queue.
This function is supposed to be called before producers start to put entities into this queue.
- Parameters
-
pn | Number of producers to this queue. |