将值添加到线程内的类的对象

时间:2019-03-24 13:24:43

标签: c++ multithreading

我想使用我的类函数addMoney向我的成员对象中添加资金,但我想在线程内执行此操作。 我怎样才能做到这一点? 我正在考虑在“ foo”方法中执行此操作,但是如何?

std::vector<std::thread> vectorOfThreads;
auto myWallet = new Wallet();

for (auto index = 0; index < 10; ++index)
{
    vectorOfThreads.push_back(std::thread(foo));
    myWallet->addMoney(1);
}

for (std::thread & th : vectorOfThreads)
{
    th.join();
}

0 个答案:

没有答案