C++ Concurrency Sandbox
Loading...
Searching...
No Matches
BoundedBlockingQueue.cpp File Reference

Implementation of a thread-safe, bounded blocking queue. More...

#include <iostream>
#include <thread>
#include <mutex>
#include <condition_variable>
#include <queue>
#include <atomic>
Include dependency graph for BoundedBlockingQueue.cpp:

Go to the source code of this file.

Classes

class  BoundedBlockingQueue
 A thread-safe queue with a fixed capacity that supports blocking operations. More...
 

Functions

void producerTask (BoundedBlockingQueue *q)
 Worker function for the Producer thread.
 
void consumerTask (BoundedBlockingQueue *q)
 Worker function for the Consumer thread.
 
int main ()
 Entry point of the program. Initializes the queue and manages the lifecycle of worker threads.
 

Detailed Description

Implementation of a thread-safe, bounded blocking queue.

  • This file demonstrates the Producer-Consumer pattern using modern C++ synchronization primitives like std::mutex and std::condition_variable.

Definition in file BoundedBlockingQueue.cpp.

Function Documentation

◆ consumerTask()

void consumerTask ( BoundedBlockingQueue q)

Worker function for the Consumer thread.

Parameters
qPointer to the shared BoundedBlockingQueue.

Definition at line 123 of file BoundedBlockingQueue.cpp.

123 {
124 for(int i=1; i<=100; ++i) {
125 int val;
126 bool pop_status = q->pop(val);
127 if(!pop_status) {
128 std::cout << "---[ConsumerTask] : SHUTDOWN DETECTED. STOPPING CONSUMPTION---" << std::endl;
129 break;
130 } else {
131 std::cout << "Consuming value..." << std::endl;
132 std::this_thread::sleep_for(std::chrono::milliseconds(100));
133 }
134 }
135 std::cout << "[Consumer] Finished my requirements! Triggering shutdown..." << std::endl;
136 q->shut_down();
137 return;
138}
bool pop(int &output_val)
Removes an element from the front of the queue.
void shut_down()
Shuts down the queue and wakes up all blocked threads.

References BoundedBlockingQueue::pop(), and BoundedBlockingQueue::shut_down().

Referenced by main().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ main()

int main ( void  )

Entry point of the program. Initializes the queue and manages the lifecycle of worker threads.

Definition at line 144 of file BoundedBlockingQueue.cpp.

144 {
145 BoundedBlockingQueue queue(5);
146
147 std::thread producerThread(producerTask, &queue);
148 std::thread consumerThread(consumerTask, &queue);
149
150 if(producerThread.joinable())
151 producerThread.join();
152 if(consumerThread.joinable())
153 consumerThread.join();
154
155 std::cout << "All work finished." << std::endl;
156 return 0;
157}
void producerTask(BoundedBlockingQueue *q)
Worker function for the Producer thread.
void consumerTask(BoundedBlockingQueue *q)
Worker function for the Consumer thread.
A thread-safe queue with a fixed capacity that supports blocking operations.

References consumerTask(), and producerTask().

Here is the call graph for this function:

◆ producerTask()

void producerTask ( BoundedBlockingQueue q)

Worker function for the Producer thread.

Parameters
qPointer to the shared BoundedBlockingQueue.

Definition at line 106 of file BoundedBlockingQueue.cpp.

106 {
107 for(int i=1; i<=30; ++i) {
108 if(!q->push(i)) {
109 std::cout << "[Producer] Shutdown detected. Stopping production." << std::endl;
110 return;
111 }
112 std::this_thread::sleep_for(std::chrono::milliseconds(25));
113 }
114 std::cout << "[Producer] Finished all items" << std::endl;
115 q->shut_down();
116 return;
117}
bool push(int val)
Adds an element to the back of the queue.

References BoundedBlockingQueue::push(), and BoundedBlockingQueue::shut_down().

Referenced by main().

Here is the call graph for this function:
Here is the caller graph for this function: