gpt4 book ai didi

c++ - 在多线程应用程序中使用智能指针

转载 作者:行者123 更新时间:2023-12-02 10:12:27 28 4
gpt4 key购买 nike

我需要执行一些计算,这取决于两个或多个步骤,如下所示:

class A
{
public:
double step1() { return 2.5; }
};

class B
{
public:
double step2() { return 1.2; }
};

class Result
{
public:
Result(std::shared_ptr<A> _a, std::shared_ptr<B> _b) : a(_a), b(_b) {};

double getResult() { return a->step1() + b->step2(); }

private:
std::shared_ptr<A> a;
std::shared_ptr<B> b;
};
实际上,第 1 步和第 2 步需要多态行为,因此这些(共享)指针将指向“接口(interface)”类,但该细节在这里并不重要。
现在, getResult() 中的最终计算还需要多态行为,所以我创建了一个指向 Result 的(唯一)指针, 创建一个调用 getResult() 的 lambda ,并将该 lambda 传递给我的线程,如下所示:
void run_multi_threaded_calculation()
{
auto result = create_result_unique_ptr();

const int nThreads = 4;
std::vector<double> save(nThreads);

auto task = [&](int n) {
// Preprocessing before getResult()
save[n] = n * result->getResult();
};

std::vector<std::thread> threads;
threads.reserve(nThreads);
for (size_t i = 0; i < nThreads; ++i)
{
threads.push_back(std::thread(task, i));
}

for (auto& th : threads)
th.join();

for (const auto& s : save)
std::cout << s << '\n';
}
问题一:我是否使用了智能指针和 lambda 捕获的正确配置,例如 unique_ptrResultshared_ptrAB ?经过一些猜测和检查更改智能指针类型后,上述编译(但如果 a 中的 bResultunique_ptr 则不编译),但我不确定这是否是最好的方法接近这个。
问题二:如果我用等效的(或者我认为的)函数对象替换 lambda,那么我的代码不会编译(错误 C2661:'std::tuple ::tuple':没有重载函数需要 2 个参数) .智能指针是否缺少我的东西,或者线程的工作方式,或者我的函数对象定义可能存在一些问题?
以下是相关更改:
class ResultFunctor
{
public:
ResultFunctor(std::unique_ptr<Result> _result, std::vector<double>& _save) : result(std::move(_result)), save(_save) {};

void operator() (int n) { save[n] = n * result->getResult(); }

private:
std::unique_ptr<Result> result;
std::vector<double>& save;
};
并替换以下行:
void run_multi_threaded_calculation()
{
// Other stuff is unchaged...

/*auto task = [&](int n) {
// Preprocessing before getResult()
save[n] = n * result->getResult();
};*/

auto task = ResultFunctor(std::move(result), save);

// other stuff is unchanged...
}

最佳答案

您遇到的部分问题是您正在传递 unique_ptr的周围。但是你错过了一个move对于在您的 ResultFunctor 类中尝试将其传递到 std::thread 的类:

threads.push_back(std::thread(task, i));
如果您必须使用 unique_ptr,那么您很可能需要在 ResultFunctor 中使用 move c'tor:
class ResultFunctor
{
public:
ResultFunctor(std::unique_ptr<Result> _result, std::vector<double>& _save) : result(std::move(_result)), save(_save) {};

void operator() (int n) { save[n] = n * result->getResult(); }

// Move c'tor rough un-tested example
ResultFunctor(ResultFunctor&& rf) :
result(std::move(rf.result)),
save(rf.save)
{};

private:
std::unique_ptr<Result> result;
std::vector<double>& save;
};
这样您就可以将其移动到 std::thread 的 c'tor 中,该 c'tor 采用 r 值 ref
threads.push_back(std::thread(std::move(task), i));
至少这是我能看到的主要问题,即使它不会导致您当前的错误。
注意:您的 lambda 与您的“等效”仿函数类完全不同。您的 lambda 是通过引用捕获的。然而,当你将它传递给线程时,这并不是真的那么安全,REAL unique_ptr 可能会超出范围并破坏它!
所以这不是一个很好的解决方案。在 c++14 中,我相信您可以使用 move 捕获 unique_ptr: How to capture a unique_ptr into a lambda expression?https://isocpp.org/wiki/faq/cpp14-language#lambda-captures

关于c++ - 在多线程应用程序中使用智能指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63001903/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com