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

A high-performance, template-based Thread Pool with Future support. More...

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

Go to the source code of this file.

Classes

class  ThreadPool_Lambda
 

Typedefs

typedef std::function< void()> Task
 

Functions

bool uploadFile (std::string name)
 Simulates a file upload operation.
 
int main ()
 Main function demonstrating ThreadPool_Lambda usage with futures.
 

Detailed Description

A high-performance, template-based Thread Pool with Future support.

  • This file implements a pool of worker threads that can execute arbitrary functions and return results asynchronously using C++11/14 features.

Definition in file ThreadPool_Lambda.cpp.

Typedef Documentation

◆ Task

typedef std::function<void()> Task

Definition at line 22 of file ThreadPool_Lambda.cpp.

Function Documentation

◆ main()

int main ( void  )

Main function demonstrating ThreadPool_Lambda usage with futures.

Definition at line 133 of file ThreadPool_Lambda.cpp.

133 {
134 ThreadPool_Lambda pool(4);
135
136 // Enqueue tasks and store the futures
137 std::future<bool> f1 = pool.enqueue(std::bind(uploadFile, "Agreement.pdf"));
138 std::future<bool> f2 = pool.enqueue(std::bind(uploadFile, "Receipt.pdf"));
139
140 std::cout << "Doing other work..." << std::endl;
141
142 // Retrieve results; these calls block until the corresponding tasks finish
143 if (f1.get() && f2.get()) {
144 std::cout << "All files uploaded successfully!" << std::endl;
145 }
146
147 return 0;
148}
bool uploadFile(std::string name)
Simulates a file upload operation.

References ThreadPool_Lambda::enqueue(), and uploadFile().

Here is the call graph for this function:

◆ uploadFile()

bool uploadFile ( std::string  name)

Simulates a file upload operation.

Parameters
nameName of the file to upload.
Returns
true on successful "upload".

Definition at line 124 of file ThreadPool_Lambda.cpp.

124 {
125 std::cout << "Uploading " << name << "..." << std::endl;
126 std::this_thread::sleep_for(std::chrono::seconds(1));
127 return true;
128}

Referenced by main().

Here is the caller graph for this function: