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

Advanced Thread Pool implementation with dynamic return types and CLI test harness. More...

#include <iostream>
#include <vector>
#include <thread>
#include <functional>
#include <mutex>
#include <queue>
#include <condition_variable>
#include <atomic>
#include <future>
#include <memory>
#include <utility>
#include <algorithm>
Include dependency graph for ThreadPool_Advanced.cpp:

Go to the source code of this file.

Classes

struct  TaskWrapper
 A functor that wraps a packaged_task shared pointer. More...
 
class  ThreadPool_Advanced
 A pool of persistent worker threads for concurrent task execution. More...
 

Typedefs

typedef std::function< void()> Task
 Type alias for a generic void-returning function. Used internally by the worker threads to process the task queue.
 

Functions

int multiply (int a, int b)
 Simulates an intensive multiplication operation.
 
std::pair< bool, int > isPrime (int n)
 Checks if a number is prime.
 
int main ()
 Main entry point to demonstrate the Request-Response pattern.
 

Variables

std::mutex log_mtx
 Global mutex for synchronized console logging. Prevents race conditions and interleaved output when multiple threads log to stdout.
 

Detailed Description

Advanced Thread Pool implementation with dynamic return types and CLI test harness.

  • This file demonstrates a sophisticated thread pool that uses std::packaged_task and std::future to facilitate asynchronous execution of various tasks with different signatures and return types.

Definition in file ThreadPool_Advanced.cpp.

Typedef Documentation

◆ Task

Type alias for a generic void-returning function. Used internally by the worker threads to process the task queue.

Alias for a generic callable object.

Alias for a callable object with a void() signature.

Type alias for a basic void function with no arguments.

Definition at line 27 of file ThreadPool_Advanced.cpp.

Function Documentation

◆ isPrime()

std::pair< bool, int > isPrime ( int  n)

Checks if a number is prime.

Parameters
nThe integer to check.
Returns
A pair containing a boolean (is_prime) and the original number.

Definition at line 160 of file ThreadPool_Advanced.cpp.

160 {
161 if (n <= 1) return {false, n};
162 for (int i = 2; i * i <= n; i++) {
163 if (n % i == 0) return {false, n};
164 }
165 return {true, n};
166}

Referenced by main().

Here is the caller graph for this function:

◆ main()

int main ( void  )

Main entry point to demonstrate the Request-Response pattern.

  • Submits various tasks to the pool and retrieves results using the future objects.

Definition at line 174 of file ThreadPool_Advanced.cpp.

174 {
175 int persistent_threads = std::thread::hardware_concurrency();
176 ThreadPool_Advanced pool(persistent_threads);
177 std::cout << "Using " << persistent_threads << " threads for the program..." << std::endl;
178
179 // Submission Phase
180 std::future<int> result1 = pool.enqueue(std::bind(multiply, 10, 5));
181 auto f2 = pool.enqueue(std::bind(isPrime, 11));
182
183 // Response Phase
184 std::cout << "Multiplication Result: " << result1.get() << std::endl;
185
186 return 0;
187}
std::pair< bool, int > isPrime(int n)
Checks if a number is prime.
int multiply(int a, int b)
Simulates an intensive multiplication operation.
A pool of persistent worker threads for concurrent task execution.

References ThreadPool_Advanced::enqueue(), isPrime(), and multiply().

Here is the call graph for this function:

◆ multiply()

int multiply ( int  a,
int  b 
)

Simulates an intensive multiplication operation.

Parameters
aFirst factor.
bSecond factor.
Returns
Product of a and b.

Definition at line 150 of file ThreadPool_Advanced.cpp.

150 {
151 std::this_thread::sleep_for(std::chrono::milliseconds(500));
152 return a * b;
153}

Referenced by main().

Here is the caller graph for this function:

Variable Documentation

◆ log_mtx

std::mutex log_mtx

Global mutex for synchronized console logging. Prevents race conditions and interleaved output when multiple threads log to stdout.

Definition at line 33 of file ThreadPool_Advanced.cpp.