libgrape-lite
A C++ library for parallel graph processing
Public Member Functions | Private Attributes | List of all members
grape::BlockingQueue< T > Class Template Reference

A concurrent queue based on condition_variables and can be accessed by multi-producers and multi-consumers simultaneously. More...

#include <concurrent_queue.h>

Public Member Functions

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
 

Private Attributes

std::deque< T > queue_
 
size_t size_limit_
 
std::mutex lock_
 
std::condition_variable empty_
 
std::condition_variable full_
 
std::atomic< int > producer_num_
 

Detailed Description

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
TType of entities in the queue.

Member Function Documentation

◆ Get()

template<typename T >
bool grape::BlockingQueue< T >::Get ( T &  item)
inline

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
itemReference of an entity to hold the got data.
Returns
If got data, return true. Otherwise, return false.

◆ Put() [1/2]

template<typename T >
void grape::BlockingQueue< T >::Put ( const T &  item)
inline

Put an entity into this queue.

This function will be blocked when the queue is full, that is, queue_.size() == size_limit_.

Parameters
itemThe entity to be put.

◆ Put() [2/2]

template<typename T >
void grape::BlockingQueue< T >::Put ( T &&  item)
inline

Put an entity into this queue.

This function will be blocked when the queue is full, that is, queue_.size() == size_limit_.

Parameters
itemThe entity to be put.

◆ SetLimit()

template<typename T >
void grape::BlockingQueue< T >::SetLimit ( size_t  limit)
inline

Set queue size. When queue_.size() == size_limit_, Putting entities will be blocked.

Parameters
limitSize limit of the queue.

◆ SetProducerNum()

template<typename T >
void grape::BlockingQueue< T >::SetProducerNum ( int  pn)
inline

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
pnNumber of producers to this queue.