C++ Concurrency Sandbox
Loading...
Searching...
No Matches
ResultQueue< T > Class Template Reference

A thread-safe template queue used for inter-thread communication. More...

Collaboration diagram for ResultQueue< T >:

Public Member Functions

void push (T result)
 Pushes a result into the queue and notifies the waiting thread.
 
pop ()
 Blocks until a result is available and then removes it from the queue.
 

Detailed Description

template<typename T>
class ResultQueue< T >

A thread-safe template queue used for inter-thread communication.

  • This acts as the "Inbox." Producers (workers) push results into it, and the Consumer (main thread) pops them out.
    Template Parameters
    TThe type of data being passed between threads.

Definition at line 26 of file MessagePassing.cpp.

Member Function Documentation

◆ pop()

template<typename T >
T ResultQueue< T >::pop ( )
inline

Blocks until a result is available and then removes it from the queue.

Returns
T The retrieved result object.

Definition at line 49 of file MessagePassing.cpp.

49 {
50 std::unique_lock<std::mutex> lock(mtx);
51 // Wait until the inbox is not empty
52 cv.wait(lock, [this] { return !this->queue.empty(); });
53 T val = std::move(this->queue.front());
54 queue.pop();
55 return val;
56 }

Referenced by main().

Here is the caller graph for this function:

◆ push()

template<typename T >
void ResultQueue< T >::push ( result)
inline

Pushes a result into the queue and notifies the waiting thread.

Parameters
resultThe data object to be sent to the inbox.

Definition at line 37 of file MessagePassing.cpp.

37 {
38 {
39 std::lock_guard<std::mutex> lock(mtx);
40 queue.push(result);
41 }
42 cv.notify_one();
43 }

Referenced by IsPrimeCalculator::operator()().

Here is the caller graph for this function:

The documentation for this class was generated from the following file: