A simple and efficient C++ thread-safe queue implementation using std::mutex and std::condition_variable. This library provides a way to enqueue and dequeue items in a thread-safe manner, making it suitable for use in multithreaded applications.
- Thread Safety: Implements a thread-safe queue using
std::mutexandstd::condition_variableto ensure safe operations in multithreaded environments. - Basic Operations: Provides
enqueueanddequeuemethods to safely add and remove items from the queue. - Flexible and Lightweight: Easy to integrate and use, with no external dependencies.
- Producer-Consumer Model: Supports use cases where multiple threads need to produce and consume items concurrently.
- Performance: Suitable for both high-performance scenarios and simple use cases.
There are two ways to use the ThreadSafeQueueLibrary in your project: by directly including the header file or by building the library and linking it with your project.
If you want to quickly integrate the ThreadSafeQueue into your project, you can simply include the header file ThreadSafeQueue.h:
-
Download or clone this repository.
-
Copy the
ThreadSafeQueue.hfile into your project. -
Include the header file in your code:
#include "ThreadSafeQueue.h"
Now you can use the ThreadSafeQueue class in your project.
You can also build this library as a static or shared library and link it to your project. This is useful if you want to reuse the library in multiple projects without copying the header file directly.
-
Clone this repository to your local machine:
git clone https://github.com/stuxtwo/ThreadSafeQueue
-
Create a build directory and configure the project with CMake:
mkdir build cd build cmake .. -
Build the library:
cmake --build .This will generate the
ThreadSafeQueuelibrary (static or shared depending on your configuration). -
Link your project with the
ThreadSafeQueuelibrary. You can do this by specifying the path to the compiled library in your build system (for example, in yourCMakeLists.txtif you're using CMake).Example of linking with CMake:
add_executable(your_project main.cpp) target_link_libraries(your_project ThreadSafeQueue)
Once the library is integrated or built, you can use it like so:
#include "ThreadSafeQueue.h"
ThreadSafeQueue<int> queue;
// Producer thread function
void producer(ThreadSafeQueue<int>& q) {
for (int i = 0; i < 10; ++i) {
q.enqueue(i);
std::this_thread::sleep_for(std::chrono::milliseconds(100)); // Simulate work
}
}
// Consumer thread function
void consumer(ThreadSafeQueue<int>& q) {
int item;
for (int i = 0; i < 10; ++i) {
if (q.dequeue(item)) {
std::cout << "Consumed: " << item << std::endl;
}
}
}
int main() {
ThreadSafeQueue<int> queue;
std::thread producer_thread(producer, std::ref(queue));
std::thread consumer_thread(consumer, std::ref(queue));
producer_thread.join();
consumer_thread.join();
return 0;
}