Skip to content

K-Adam/SafeQueue

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 

Repository files navigation

SafeQueue

This class is a multi-producer, multi-consumer queue. It offers both blocking and non-blocking consuming as well, while producing is always blocking. The capacity is dynamically adjusted. The implementation is based on std::queue, and the thread safety is achieved using std::mutex and std::condition_variable.


The Produce(T&& item) function can be called with a rvalue reference. The Consume(T& item) function returns immediately with a boolean value, indicator of success. The ConsumeSync(T& item) method will block the calling thread until it can take an item from the queue. The Finish method stops all the waiting consumers (ConsumeSync), and make them return false.

Example

Producer:

SafeQueue<MyType> queue;

// insert elements
queue.Produce(std::move(var1));
queue.Produce(std::move(var2));

Non-blocking consumer:

SafeQueue<MyEvent> event_queue;

// Game loop
while(!quit) {

	MyEvent event;
	while(event_queue.Consume(event)) {
		// Process the event
	}

	UpdateWorld();
	Render();
}

Blocking consumer:

SafeQueue<MyMessage> message_queue;

std::thread consumer([&](){
	MyMessage message;
	while(message_queue.ConsumeSync(message)) {
		// Process the message
	}
});

// ...

About

A thread-safe queue implementation in C++

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages