#include <iostream>
#include <vector>
#include <future>
class A {
public:
A(int value) : value_(value) {}
// Copy constructor
A(const A& other) : value_(other.value_) {
std::cout << "Copy constructor called\n";
}
// Move constructor
A(A&& other) noexcept : value_(other.value_) {
other.value_ = 0; // Reset the moved-from object
std::cout << "Move constructor called\n";
}
int value() const { return value_; }
private:
int value_;
};
void process(const A& obj) {
// Simulate some work
std::this_thread::sleep_for(std::chrono::milliseconds(100));
std::cout << "Processing object with value: " << obj.value() << std::endl;
}
int main() {
std::vector<A> objects;
objects.push_back(A(1));
objects.push_back(A(2));
objects.push_back(A(3));
std::vector<std::future<void>> futures;
std::cout << "Start processing\n";
for (const auto& obj : objects) {
// Capture 'obj' by reference in the lambda
futures.push_back(std::async(std::launch::async,
[&obj]() { process(obj); }));
}
// Wait for all threads to finish
for (auto& f : futures) {
f.get();
}
return 0;
}