可能重复:
Start thread with member function
我有个小班:
class Test
{
public:
void runMultiThread();
private:
int calculate(int from, int to);
} 如何使用两个不同的参数集(例如,calculate、calculate(11,20))在方法runMultiThread()的两个线程中运行方法runMultiThread()
谢谢我忘了我需要传递this作为参数。
发布于 2012-06-12 14:32:03
没那么难:
#include <thread>
void Test::runMultiThread()
{
std::thread t1(&Test::calculate, this, 0, 10);
std::thread t2(&Test::calculate, this, 11, 20);
t1.join();
t2.join();
}如果仍然需要计算结果,则使用未来:
#include <future>
void Test::runMultiThread()
{
auto f1 = std::async(&Test::calculate, this, 0, 10);
auto f2 = std::async(&Test::calculate, this, 11, 20);
auto res1 = f1.get();
auto res2 = f2.get();
}https://stackoverflow.com/questions/10998780
复制相似问题